In interface testing, most projects have interfaces that require user login to operate, and the python requests module is often used to simulate login and post-login operations. In this article, I will show you two methods to handle login certificate cookies use python requests module.
1. Use requests.utils.dict_from_cookiejar() Method To Converts Returned Cookies Into A Dictionary.
- Get server returned cookies.
# Import python requets module. import requests def login_return_cookie(): # The website login url. login_url = 'http://www.test-abc.com/login' # Custom request headers. headers = { "Accept": "application/json, text/javascript, */*; q=0.01" } # The login form submit data. login_data = { "username": "jerry", "password": "888888" } try: # Send a post request to login_url with login_data. response = requests.post(url=login_url, headers=headers, data=login_data) # Get server response cookies. cookies = response.cookies # Convert cookies to a dictionary object. cookie = requests.utils.dict_from_cookiejar(cookies) # Return the dictionary object. return cookie except Exception as err: # When there are exceptions, print error message. print('Retrieve cookie fail:\n{0}'.format(err))
- Use server returned cookies for later process.
import requests # This function will use server returned cookie for later url request. def get_data(): # First invoke login_return_cookie() method to login to the website and get server returned cookies value. cookie = login_return_cookie() # Request below url to vote, the vote action need login cookies. vote_url = 'http://www.test-abc.com/vote' # Pass server returned cookies to the vote_url request. response = requests.get(url = vote_url, cookies=cookie) print(response.text)
2. Iterate Cookies Key-Value Pairs And Concat Them Into Cookie Format String.
- Get server returned cookies.
# Import python requets module. import requests def login_return_cookie_string(): # The website login url. login_url = 'http://www.test-abc.com/login' # Custom request headers. headers = { "Accept": "application/json, text/javascript, */*; q=0.01" } # The login form submit data. login_data = { "username": "jerry", "password": "888888" } try: # Send a post request to login_url with login_data. response = requests.post(url=login_url, headers=headers, data=login_data) # Get server response cookies item. cookies = response.cookies.items() cookie = '' # Iterate the server returned cookies. for key, value in cookies: # Concat cookie string use the key=value; cookie format. cookie += '{0}={1};'.format(key, value) # Return the dictionary object. return cookie except Exception as err: # When there are exceptions, print error message. print('Retrieve cookie fail:\n{0}'.format(err))
- Use above cookie string to send requests.
import requests def get_data(): # Get server response cookie string. cookie_string = login_return_cookie_string() # Set the cookie string in http request headers. headers = { "cookie": cookie_string } # Send get request with above custom headers(contains the cookies). response = requests.get(url=get_data_url, headers=headers) print(response.text)