Shane Shane - 1 year ago 71
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 = ""
LoginHeader = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)", "referer": ""}
LoginData = "example=&hello=hi"

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

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download