I am trying to get around a certain service that does not have an API and decided to try Mechanize (usually I use urllib).
How to add a specific header for one open call?
Or is there a way to create an instance of Request with its own headers and then process its instance of mechanize.Browser ?
browser = mechanize.Browser() headers = [ ('Accept', 'text/javascript, text/html, application/xml, text/xml, */*'), ('Content-type', 'application/x-www-form-urlencoded; charset=UTF-8'), ('User-Agent', 'Foobar'), ] browser.addheaders = headers # log in, do stuff, etc. # here, for this one browser request, I need to add an AJAX header browser.open('/a_url_to_ajax_post/', urllib.urlencode({'foo': 'bar'}))
My workaround is to temporarily change the addheaders list, but wow is not ugly!
browser.addheaders.append(AJAX_HEADER) browser.open('/admin/discounts', urllib.urlencode(pulled_params)) browser.addheaders.pop()
source share