How to set user name and password in a HTTP get request?

4.9k Views Asked by At

I have use requests library from Python to send a get request:

response = requests.get(url, verify, auth, params, headers)

My assumption was that one can send any get-request directly from a browser. For example, in the address line of a browser I need to put something like that:

http://my_url.net?param1=12&param2=777

In the above way I can set url and params that I use in my Python function. But how can I set auth (user name and password) in the get request in case if I use browser to send the request. The same question is applicable to the remaining two arguments: verify and headers.

2

There are 2 best solutions below

1
On

Depending on your browser, I guess you can find extensions that can help you do that.

If you use cUrl, you can do something like :

curl http://my_url.net?param1=12&param2=777 --header "Authorization: Basic XXX"

where "xxx" is the Base64 encoding of the string "username:password".

2
On

Or try this, using a password manager with a default realm:

url = "http://my_url.net?param1=12&param2=777"

pw_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
pw_mgr.add_password(None, url, username, password)

urllib2.install_opener(
    urllib2.build_opener(urllib2.HTTPBasicAuthHandler(pw_mgr)))

request = urllib2.Request(url)
response = urllib2.urlopen(request)