How to send post in python

(*-*)浩
Release: 2019-07-09 10:10:56
Original
5426 people have browsed it

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:

How to send post in python

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)
Copy after login

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)
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!