curl error 18 - transfer closed with outstanding read data remaining


Question

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?

1
61
3/1/2019 9:55:45 AM

Accepted Answer

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.

30
3/5/2019 4:39:00 PM

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 with the request unmodified, 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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon