jwagenleitner / groovy-wslite

Lightweight SOAP and REST webservice clients for Groovy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to set content-length?

futureboy37 opened this issue · comments

I'm having a bear of a time getting my rest client to set the content length for my request, and the server is throwing back HTTP error code 411 (length required) without it. I've tried everything... even 4 Content-Length headers:

def response = client.post(path:'/path/to/service', accept: ContentType.JSON) {
  type ContentType.JSON
  query:[address: location]
  headers: ["Content-Length":0,"Content-Length":"0", "ContentLength":"0", "ContentLength":0]
}   

Nothing works. How can I accomplish this?

If you want to specify your own Content-Length you would do like this:

client.post(path:'/path/to/service', 
                     accept: ContentType.JSON, 
                     headers: ['Content-Length':1024]) 
{
....
}

The query and headers params need to go in the method arguments and not in the closure body.

I tried that, see the example I posted. Perhaps the problem is I set the content-length to 0? But in my case I didn't actually post any data, just set the content type and the query parameters, but no actual form content was sent. Maybe there is some other data being sent? Is there an easy way to get the content length if I'm not intending to actually send data?

In the example you posted you have the headers in the closure body, it doesn't belong there. See my example of where to place it.

The Content-Length header should only be set if the request contains data. If you aren't posting data it shouldn't be added.

https://github.com/jwagenleitner/groovy-wslite/blob/master/src/main/groovy/wslite/http/HTTPClient.groovy#L113