Interface testing - Requests library GET request

The Requests library GET request uses the GET request method in the HTTP protocol to initiate a request to the target website.

(For GET requests without parameters, please see the exercise in the previous article)

1. GET request with pending parameters in Requests library

When using the Get method to request with parameters, params=parameter dictionary, not data=parameter dictionary. The data=parameter dictionary is the parameter of the post method.

"""
1.学习目标
    必须掌握requests中带参数的get请求
2.语法
    requests.get(url,parmas=None)
    2.1 不带参数的get请求(看上一篇中的练习)
    2.2 带参数的get请求(如下)
3.操作步骤
    # 1.导入requests库
    # 2.明确请求地址
    # 3.明确请求参数
        data = {key:value}  字典格式
    # 4.发送请求获取返回数据
4.需求
    使用requests库发送一个带参数的get请求
"""
# 1.导入requests库
import json
import requests

# 2.明确请求地址
url = "http://127.0.0.1:8000/api/departments/"
# 3.明确请求参数
# 是一个字典类型数据
data = {"$dep_id_list": "10,11"}
# 4.发送请求
response = requests.get(url=url, params=data)
# print(response.json())

# 将python对象转换为json字符串(格式化放回数据)
result = json.dumps(response.json(), indent=2, ensure_ascii=False)
# print(type(result))  # 字符串类型
print(result)


"""
结果:
{
  "count": 2,
  "next": null,
  "previous": null,
  "results": [
    {
      "dep_id": "10",
      "dep_name": "超神_10",
      "master_name": "陆兴雷_10",
      "slogan": "带头学习"
    },
    {
      "dep_id": "11",
      "dep_name": "超神_11",
      "master_name": "陆兴雷_11",
      "slogan": "带头学习"
    }
  ]
}

"""

2. View the content of the GET request

We can Httpbinview or debug the content of the request sent by the Requests library by accessing it.

Let’s take the above example as an example:

# 1.导入requests库
import json
import requests

# 2.明确请求地址
# url = "http://127.0.0.1:8000/api/departments/"
url = "http://127.0.0.1:9999/get"
# 3.明确请求参数
# 是一个字典类型数据
data = {"$dep_id_list": "10,11"}
# 4.发送请求
response = requests.get(url=url, params=data)
# response = requests.get(url='http://httpbin.org/get?name=admin&age=20')  # 这种方式调用也可以
# print(response.json())

# 将python对象转换为json字符串(格式化放回数据)
result = json.dumps(response.json(), indent=2, ensure_ascii=False)
# print(type(result))  # 字符串类型
print(result)


"""
接口返回结果:
{
  "args": {
    "$dep_id_list": "10,11"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "keep-alive",
    "Host": "127.0.0.1:9999",
    "User-Agent": "python-requests/2.18.4"
  },
  "origin": "127.0.0.1",
  "url": "http://127.0.0.1:9999/get?%24dep_id_list=10%2C11"
}

我们可以看到args属性中有参数内容,
如果是不带参数的Get请求,args属性中是没有内容的,如下:

{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "keep-alive",
    "Host": "127.0.0.1:9999",
    "User-Agent": "python-requests/2.18.4"
  },
  "origin": "127.0.0.1",
  "url": "http://127.0.0.1:9999/get"
}
"""

3. Get request with request header and parameters

For example, in the above example, in the request header information "User-Agent": "python-requests/2.18.4", we need to change User-Agentthe content of the attributes in the request header. See the following example:

# 1.导入requests库
import json
import requests

# 2.明确请求地址
# url = "http://127.0.0.1:8000/api/departments/"
url = "http://127.0.0.1:9999/get"
# 3.明确请求参数
# 是一个字典类型数据
data = {"$dep_id_list": "10,11"}

# 明确请求头信息
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3775.400 QQBrowser/10.6.4209.400"}

# 4.发送请求
response = requests.get(url=url, params=data, headers=headers)
# print(response.json())

# 将python对象转换为json字符串(格式化放回数据)
result = json.dumps(response.json(), indent=2, ensure_ascii=False)
# print(type(result))  # 字符串类型
print(result)

"""
接口返回结果:可以看到User-Agent属性变成了我们设置的内容了。
{
  "args": {
    "$dep_id_list": "10,11"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "keep-alive",
    "Host": "127.0.0.1:9999",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3775.400 QQBrowser/10.6.4209.400"
  },
  "origin": "127.0.0.1",
  "url": "http://127.0.0.1:9999/get?%24dep_id_list=10%2C11"
}

"""

In addition to the Get request, there are many request parameters. For example, timeoutyou can set the request time. If it exceeds this time, the request will end automatically. You can use this to determine the corresponding efficiency of the request agent and avoid wasting too much time on some erroneous requests.

Finally, I would like to thank everyone who has read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/myh919/article/details/132830654