Python post 时需要 提交JSON 数据的情况 - 解

a 夏天 提交于 2020-02-19 10:56:55
来,先上代码:
import requests
import json

url = "http://example.com"
data = {
    'tt': 1,
    'gg': 2,
}

# 以下有两种 : 
# 1。data参数传
requests.post(url, data=json.dumps(data))
# 2。json参数传
requests.post(url, json=data)
完。---知识点1: Reqeusts支持以form表单形式发送post请求,只需要将请求的参数构造成一个字典,然后传给requests.post()的data参数即可。知识点2: Requests也支持以multipart形式发送post请求,只需将一文件传给requests.post()的files参数即可。
url = 'http://httpbin.org/post'
files = {'file': open('report.txt', 'rb')} 
r = requests.post(url, files=files)
print r.text

 

知识点3: 简单 测试 post 的服务器 ,可以用 httpbin.org 来测试结果。---
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!