简体   繁体   中英

Python requests module: Authenticate and post urlencoded information

I am successfully able to urlencode my JSON information, but I cannot POST my urlencoded information because I need to authenticate when posting.

How can I do this?

My code is as follows:

import urllib
import pycurl
import certifi
import json
import requests
from requests.auth import HTTPBasicAuth

requests.post('https://website.com/update/', auth=('user', 'pass'))

uri = 'https://website.com/update/'
params = {}
data= {"simId":760590802,"changeType":2,"targetValue":000307,"effectiveDate":'null'}
params["data"] = json.dumps(data)
r = requests.post(uri, data=params)
print r.text

As you can see, I am able to post my information. But upon doing so, I need to authenticate myself before fully doing so.

I'm browsed the requests module authentication page, but none of those authentication methods worked. This works for pycurl whereas I can set my username and password, so how do I do the same with the requests module?

Since you mentioned username and password, the authentication method is most likely HTTP Basic Authentication which can be easily done with requests like this:

from requests.auth import HTTPBasicAuth
requests.post('https://website.com/update/', auth=HTTPBasicAuth('user', 'pass'))

If your application has some sort of frontend you can already access with a browser, try developer tools on Chrome to see how the authentication is done (method, parameters) and try to replicate that.

Afterwards you can most likely use the requests session object. This will help you authenticate only once and not for every request you make.

http://docs.python-requests.org/en/v1.0.4/user/advanced/#session-objects

Open a new session:

s = requests.Session()

Do your auth using the session; this is just an example, you could do your auth however your app supports(a post request for example)

s.auth = ('user', 'pass')

If you have any session settings (cookies, headers, etc) you have to add them to your session.

s.headers.update({'x-test': 'true'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM