jgivoni jgivoni - 1 year ago 67
PHP Question

PHP curl_exec returns both HTTP/1.1 100 Continue and HTTP/1.1 200 OK separated by space

I'm calling a service from PHP using cURL, like this:

$response = curl_exec($ch);

and the request/response headers look something like this:


POST /item/save HTTP/1.1
Host: services.mydomain.com
Accept: */*
Content-Length: 429
Expect: 100-continue
Content-Type: multipart/form-data


HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Date: Fri, 06 Jul 2012 08:37:01 GMT
Server: Apache
Vary: Accept-Encoding,User-Agent
Content-Length: 256
Content-Type: application/json; charset=utf-8

followed by the body (json encoded data).

The problem is that the common thing is to split headers and body in the response by the first empty line encountered, except in this case, the empty line is after the
100 Continue
and therefore everything else gets pushed into the body‚Äďand that is not valid json anymore :-)

So my question is this: What's the common way to deal with this?
I have 3 options lined up:

  1. Specify that curl should not expect
    ? (How?)

  2. Specify that curl should only send back the headers of the last response? (How?)

  3. Manually check for
    100 Continue
    headers and disregard them and their following empty line? (In that case, are there other similar things that could happen, that I should manually check for?)

Unless I'm missing something obvious, I'm sure people have stumbled upon this and solved it many times!

ts. ts.
Answer Source

I will opt for #1. You can force curl to send empty "Expect" header, by adding:

curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:"));

to your code

If you want check it manually, you should define your own header callback and maybe write callback (look for CURLOPT_HEADERFUNCTION and CURLOPT_WRITEFUNCTION in curl_setopt doc), which has simply to ignore all "HTTP/1.1 100 Continue" headers.