Shane Shane - 9 days ago 6
Python Question

400 bad request error when trying to do a HTTP POST using python mechanize

I've checked the headers info using Live HTTP headers when doing an HTTP POST in firefox, and then I try to do this using mechanize which brought me 400 bad request error. Heres my code:

MechBrowser = mechanize.Browser()

LoginUrl = "http://example.com"
LoginHeader = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)", "referer": "http://example.com"}
LoginData = "example=&hello=hi"

LoginReq = urllib2.Request(LoginUrl, LoginData, LoginHeader)
LoginResposne = MechBrowser.open(LoginReq)


Variable is copied from LiveHTTPHeaders, and I still get 400 error. Is there something missing?

Answer

There are actually hidden info required for that request which was not captured by LiveHTTPHeaders, so I turned to fiddler for more detailed reports and analysis, problem solved.