cURL POST of custom binary data (not form contents)

The problem the following code piece successfully sends POST request but doesn't send the data in $sendStream (the stream is valid and contains data - this has been verified):

curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-type: application/x-rethync-request'));
curl_setopt($request, CURLOPT_HEADER, true);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_INFILE, $sendStream);
curl_setopt($request, CURLOPT_INFILESIZE, stream_length($sendStream));
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "POST");

$response = curl_exec($request);

I've read probably all cURL POST-related posts here on SO, but no luck. Why is the data not posted?

Upvotes: 3

Views: 3612

Answers (1)

The missing piece was

    curl_setopt($request, CURLOPT_UPLOAD, 1);

The PHP documentation mentions this option briefly and nothing suggests that the option should be set. Still at least some (if not all) versions of cURL don't post data despite it's specified via CURLOPT_INFILE, unless CURLOPT_UPLOAD is also set.

NOTE: when you use this method to send the data, the response header will contain HTTP/1.1 100 Continue first, followed by HTTP/1.1 200 OK and so on. So when you parse response headers, beware of the first 100 Continue response (you'll need to strip it).

Upvotes: 10

Related Questions