I’m trying to get around a certain service not having an API and decided to try Mechanize (I normally use urllib).
How do I add a specific header for one open call?
Or is there a way to construct a Request instance with its own headers, then have my mechanize.Browser instance handle it?
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 modify the addheaders list, but wow that is ugly!
browser.addheaders.append(AJAX_HEADER)
browser.open('/admin/discounts', urllib.urlencode(pulled_params))
browser.addheaders.pop()
Do it like this: