0

I am trying to automate a web page request by using mechanize in python. When I add custom headers like
X-Session= 'abc'
and
X-Auth='123'
by using addheader function.

object=mechanize.Browser()
object.addheaders=[('X-Session','abc'),('X-Auth','123')]

It changes those headers to X-session and X-auth. I believe due to that the server is not able to authenticate me. Can anybody help how to maintain the case? Thanks.

  • http://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive says that http headers are case-insensitive. What makes you say it could be due to case that server isnt authenticating your request? – shahkalpesh Apr 10 '17 at 08:23
  • Yes right, but since it is a custom header the implementation on the server side may not deal with case insensitivity. It might be checking with the original header. – Vaibhav Agrawal Apr 10 '17 at 17:47

1 Answers1

0

Mechanize expect two items tuple as header, first item is header name, second is value, so you must do:

object.addheaders=[('X-Session','abc'), ('X-Auth','123')]

(Two tuples of two elements instead of one tuple with 4 elements).


To check headers that Mechanize will send with query, you can do:

print(request.header_items())

This should print something like:

[('X-Session','abc'), ('X-Auth','123')]

Doc: http://wwwsearch.sourceforge.net/mechanize/doc.html#adding-headers

Arount
  • 9,853
  • 1
  • 30
  • 43
  • Hi, thanks for your response and yes i had make it like a tuple only as you stated above, I will update the code which is a typo. Also when i print request.header_items() it shows "X-Session" as "X-session". – Vaibhav Agrawal Apr 10 '17 at 17:50