I’m creating a SublimeText 2 plugin that posts data to a server I run. I wrote the basics while on the train using my phone as a WiFi hot spot and everything worked fine. Then when I got home I got a 400 Bad Request response from my server no matter what I tried to post. I put my laptop back on my personal hot spot and the error went away. Here are the details:
The “client” in this case is Sublime Text 2. If you don’t already know, their plugins are written in Python and I’m using urllib, urllib2, and httplib to handle requests. Here is the relevant Python that makes the request:
params = urllib.urlencode({'title': 'ST2 Note', 'content': data, 'user': user, 'pass': pswd})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection("staging.myserver.me:80")
conn.request("POST", "staging.myserver.me/st2", params, headers)
response = conn.getresponse()
data2 = response.read()
print data2
conn.close()
The code above sends a POST request just fine. If I’m on a certain connection my server definitely understands it because I set it to just echo back what I send in the POST data. The request is being made to part of a PHP (Codeigniter) application and yes, I’ve specifically set it up so that CSRF protection is off for this particular URL so I know that’s not the issue. The PHP code itself is rather uninteresting but I set it to echo back the server headers and this is what it sent when I made the request from the connection that works:
Host: staging.myserver.me
Accept-Encoding: identity
Content-Length: 56
Content-type: application/x-www-form-urlencoded
Accept: text/plain
Via: HTTP/1.1 akrmspsrvz9ts212.wnsnet.attws.com
Any ideas why the server understands requests from some connections but not others?
Looks like I got it working. I KNOW there are others with my problem out there so here’s what happened…
For reasons that I’m too much of a Python/HTTP1.1 newbie to understand, a plan hostname without “http://” in front of it works some of the time. Must have something to do with ISPs and how they route traffic, not sure. So the fix was do modify this line:
The above line caused problems. But changing it to this:
Got it to work instantly! I hope this helps someone down the line. 400 errors are almost as mysterious as 500 errors sometimes.