Wednesday, October 29, 2014

php - Curl POST - 411 Length Required



We have a RestFUL API we build in PHP. If we make the request:




curl -u api-key:api-passphrase https://api.domain.com/v1/product -X POST


We get back:



411 - Length Required


Though if we simply add -d "" onto the request it works and no 411 error. Is there a way to not require adding -d to the curl command?




We are using lighttpd web server, and believe its lighttpd NOT php who is returning the 411 error.


Answer



You are correct -- lighttpd doesn't support POST requests with an empty message body without a 'Content-Length' header set to zero, and CURL sends such a request. There's argument back and forth about who's right, but in my opinion, lighttpd is broken. A POST with no Content-Length and no Transfer-Encoding is perfectly legal and has no message body.



Adding -d "" causes CURL to send a Content-Length: 0 header, which resolves the problem.



You could modify lighttp. Find the code that issues the 411 error and instead set the content length to zero.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...