0

In my project

I used

curl -I url -H"Content-Type: application/octet-stream" -X POST

everything works fine.

But in python I used requests module

response = requests.post(url, data="mystring", headers="Content-Type: application/octet-stream")

Then I got 400 error, which means: Bad request; the request was somehow malformed and could not be processed.

what could be the problem?

Jimmy
  • 113
  • 3
  • 15

2 Answers2

0

Are you sure url and data are exactly the same for curl and requests?

John Hua
  • 1,400
  • 9
  • 15
  • Yes, Url was copy pasted. Since there's not even data in curl command, it gives me right feed back. What could be the difference? – Jimmy Jan 29 '15 at 01:15
0

try some basic troubleshooting. assuming the url and data string are correct. are you sure you are able to get to the server? a curl request is a terminal command and connects differntly than python API type commands, python might need extra permissions opened up by your server. if you are running Apache you would need to make sure it is allowing for scirpted headers

read this: http://httpd.apache.org/docs/2.2/mod/mod_headers.htmlhttp://httpd.apache.org/docs/2.2/mod/mod_headers.html

also try an alternate way of connecting using other libraries like the person on this thread did: How do you send a HEAD HTTP request in Python 2?

" use httplib

import httplib
conn = httplib.HTTPConnection("www.google.com")
conn.request("HEAD", "/index.html")
res = conn.getresponse()
print res.status, res.reason
200 OK
print res.getheaders()
[('content-length', '0'), ('expires', '-1'), ('server', 'gws'), ('cache-control', 'private, max-age=0'), ('date', 'Sat, 20 Sep 2008 06:43:36 GMT'), ('content-type', 'text/html; charset=ISO-8859-1')]

"

Community
  • 1
  • 1
castaway2000
  • 306
  • 5
  • 21
  • I tried the python3 http.client module and it ended up with the same result. And the server is Nginx. Could it be possible that the problem comes actually from the server side ? – Jimmy Jan 30 '15 at 10:22
  • it is very possible that the problem is from the server side settings. look into seeing if this documentation here gives you the awnsers you are looking for. http://nginx.org/en/docs/http/ngx_http_core_module.html – castaway2000 Feb 02 '15 at 19:13