Home > Article > Backend Development > How to send post in python
When we used postman for interface testing, we found that there are three encoding methods for POST requests. The specific encoding methods are as follows:
A: application/ x-www-form-urlencoded ==The most common way to submit data in a post, submit data in form form
B: application/json ==Submit data in json format (recommended learning: Python Video tutorial)
C: multipart/form-data == Generally used to upload files (less commonly used)
When we use python for interface testing, the commonly used method is :requests.post(url,data), specifically we use different encoding methods for interface testing:
A: Requests sends post requests in the form of a form. The specific code implementation is as follows:
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)
B: Requests sends post requests in json form. The specific code implementation is as follows:
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)
C: Requests sends post in multipart form Request, the specific code implementation is as follows:
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)
Note: E://report.txt==Customized, specifically defined according to the directory where you put it, the content is arbitrary
For more Python related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to send post in python. For more information, please follow other related articles on the PHP Chinese website!