current position:Home>Python interface automation - post request parameters are passed to JSON or data. How to distinguish

Python interface automation - post request parameters are passed to JSON or data. How to distinguish

2022-02-02 12:42:35 Frozen milk coffee

One 、 How to send post request

1、 Interfaces to be tested url
2、 Interface request parameters (header、 param)

The code is as follows ( Example ):

import requests
url = "http://v.juhe.cn/exp/index"
header = {
    
"content-type”:"application/json"
}
param = {
    
 "key":"5c14a269******81ee388a"
 "com":888,
 "no":8928288282111
}
resp = requests.post(url,headers=header,json=param)
print(resp.text)

Two 、post How to pass in the parameters of

In general , Use request Reference time , It will pass in data perhaps json
 Insert picture description here
When will it be transmitted data, When will it be delivered json?
1、 According to the interface request header header To judge , If content-type by application/json, by json Format , Then use json Parameters

Method 1 :
Direct transmission json
The code is as follows ( Example ):

import requests
url = "http://v.juhe.cn/exp/index"
header = {
    
"content-type”:"application/json"
}
param = {
    
 "key":"5c14a269******81ee388a"
 "com":888,
 "no":8928288282111
}
resp = requests.post(url,headers=header,json=param)
print(resp.text)

Method 2 :
First convert the parameter to json character string , Direct transmission data
The code is as follows ( Example ):

import requests
import json
url = "http://v.juhe.cn/exp/index"
header = {
    
"content-type”:"application/json"
}
param = {
    
 "key":"5c14a269******81ee388a"
 "com":888,
 "no":8928288282111
}
# Turn the dictionary into json strand 
params = json.dumps(param)
resp = requests.post(url,headers=header,data=params)
print(resp.text)

2、 If content-type by application/x-www-form-urlencoded, Format as form , Then use data Parameters
The code is as follows ( Example ):

import requests
url = "http://v.juhe.cn/exp/index"
header = {
    
"content-type”:"application/x-www-form-urlencoded"
}
param = {
    
 "key":"5c14a269******81ee388a"
 "com":888,
 "no":8928288282111
}
resp = requests.post(url,headers=header,data=param)
print(resp.text)

copyright notice
author[Frozen milk coffee],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202021242330887.html

Random recommended