SoFunction
Updated on 2024-11-20

Python interface test get request process in detail

The python excuses test uses the requests module, first of all, you need to import the requests library, pip install requests

1、get direct request method

Take Douban as an example:

url = '/'
respose = (url=url)
# status_code is the returned status code
print(respose.status_code)
# text is the returned data
print()

Request result: returns a status code of 200, indicating that the requested server responds normally, but does not indicate that the interface is normal

To determine the interface is normal, look at the return data, if the return data to meet the expected results to be considered normal interface

2、get send parameter test

Request Code:

Requesting Douban's search function, searching for data for three bodies

The parameters sent must be in the form of a dictionary, and multiple parameters can be sent. Format: {'key1':value1', 'key2':'value2', 'key3', 'value3'}

The following code requests for; url+parameters, want to be equal to the request url=/search? q=tribe

# --*-- coding:utf-8 --*--
import requests
search_url = '/search'
search_word = {'q':'trisomy' (medical term)}

respose = (url=search_url, params=search_word)

print(respose.status_code)
print()

Response Results:

You can see that the response data has bean reading search: three bodies

3、Response results of the return information

status_code:response status code

url: the url of the request

encoding: encoding format

headers: response headers

request: the way the request was made

cookies: cookie data

raw: returns the original response body

Other return information:

content: usually used for compression such as gzip, will customize the decompression

json(): built-in json decoder

This is the whole content of this article.