tags:

views:

553

answers:

3

Hello,

when retrieving data from a URL using curl, I sometimes (in 80% of the cases) get

error 18: transfer closed with outstanding read data remaining

Part of the returned data is then missing. The weird thing is that this does never occur when the CURLOPT_RETURNTRANSFER is set to false, that is the curl_exec function doesn't return the data but displays the content directly.

What could be the problem? Can I set some of the options to avoid such behaviour?

Many thanks for your suggestions!

+2  A: 

I bet this is related to a wrong Content-Length header sent by the peer. My advice is to let curl set the length by itself.

SleepyCod
No, this error has nothing to do with what curl sends in its Content-Length: request header.
Daniel Stenberg
It can be related to Content-Length response header. I have encountered a similar case in one of coworker projects: a Java webservice gateway sets Content-Length: 601 while the XML response is 210 bytes
pcdinh
A: 

I had the same problem, but managed to fix it by suppressing the 'Expect: 100-continue' header that cURL usually sends:

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));

By the way, I am sending calls to the HTTP server that is included in the JDK 6 REST stuff, which has all kinds of problems. In this case, it first sends a 100 response, and then with some requests doesn't send the subsequent 200 response correctly.

Christopher Sahnwaldt
A: 

The error string is quite simply exactly what libcurl sees: since it is receiving a chunked encoding stream it knows when there is data left in a chunk to receive. When the connection is closed, libcurl knows that the last received chunk was incomplete. Then you get this error code.

There's nothing you can do to avoid this error, but you can try to work around it by issuing a HTTP 1.0 request instead (since chunked encoding won't happen then) but the fact is that this is most likely a flaw in the server or in your network/setup somehow.

Daniel Stenberg