HTTP error 417 with PHP’s curl March 11, 2010

I’ve been getting 417 response codes (Expectation failed) while making requests with PHP/curl to tinyurl.com (I’ll probably publish that project here in a week or two). Well, its very annoying. took me some time to figure that one out.

If someone else wonders about it, that’s the deal:

If the content length (of the request post data) is bigger than 1KB (>=1025 bytes), the client (curl in our case) sends the request with a “Expect: 100-continue” header and without the actual post data, than awaits for a response with status 100, and than finally sends the actual post data.

The problem is that many servers (lighttpd 1.4 among them, fixed at 1.5) doesn’t support that, which is exactly why I got the 417 error when my client attempted to use that header.

To make curl suppress that header, and just send the request/post data normally while ignoring the fact that the post data is more than 1KB, add the following piece of code:

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

Adding a header with no value makes it overwrite the original value and suppress the header, which should make everything work fine.

It should be noted that every time you set CURLOPT_HTTPHEADER it overwrites the original HTTPHEADER value and not appends to it, so if you already set it, you should simply add ‘Expect:’ to your existing HTTPHEADER array instead of adding the entire line.

4 Comments
Nicolas Martin April 15th, 2010

Thanks for sharing this ! I just encountered some strange 417 codes and your post saved my day !

admin April 24th, 2010

No problem, I’m glad it helped someone out there :)

BTW, you had a type in your URL, I fixed it

Shesek

Nicolas Martin May 27th, 2010

I’m also encountering some strange 400 (Bad request) error codes, mainly over ligthttp/1.4.26 servers.

Any ideas ?

Ethan Tuttle June 19th, 2010

Oh man this was a huge help just now. Thanks.

Leave a Reply