1. We use a test interface postman and found encoded POST request There are three ways, the specific code as follows:
A: mode application / x-www-form-urlencoded == most common post submitted data, data submitted in the form form form
B: application / json == submit data in the format json
C: multipart / form-data == generally used to upload files (less common)
2. When do we use python interface test, frequently used for: requests.post (url, data), we use different encoding specific way to do interface testing:
A: Requests sent a request to post form form form, the specific code implementation is as follows:
1
2
3
4
5
6
7
8
|
import
requests,json
url
=
'http://httpbin.org/post'
data
=
{
'key1'
:
'value1'
,
'key2'
:
'value2'
}
r
=
requests.post(url,data)
print
(r)
print
(r.text)
print
(r.content)
|
A1: Run results are shown below:
B: Requests sent json post request form, the specific code implementation is as follows:
1
2
3
4
5
6
7
8
|
import
requests,json
url_json
=
'http://httpbin.org/post'
data_json
=
json.dumps({
'key1'
:
'value1'
,
'key2'
:
'value2'
})
#dumps:将python对象解码为json数据
r_json
=
requests.post(url_json,data_json)
print
(r_json)
print
(r_json.text)
print
(r_json.content)
|
B1: Run results are shown below:
C: Requests sent a request to post multipart forms, specific code implementation is as follows:
1
2
3
4
5
6
7
8
|
import
requests,json
url_mul
=
'http://httpbin.org/post'
files
=
{
'file'
:
open
(
'E://report.txt'
,
'rb'
)}
r
=
requests.post(url_mul,files
=
files)
print
(r)
print
(r.text)
print
(r.content)
|
C1: Run results are shown below:
Note: E: //report.txt== custom, defined according to their specific place in the catalog, content freely