脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python使用requests POST提交一个键多个值方式

python使用requests POST提交一个键多个值方式

2022-09-25 11:35win_turn Python

这篇文章主要介绍了python使用requests POST提交一个键多个值方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

使用requests POST提交一个键多个值

问题

在使用POST提交数据时,想实现下面这种情况:

?
1
requests.post(url, data={'interests':'football','interests':'basketball'})

用这种方式肯定是错误的,因为字典中的key是唯一的。

解决方法

使用元组列表

代码如下:

?
1
2
3
4
import requests
url = 'http://httpbin.org/post'
r = requests.post(url, data=[('interests', 'football'), ('interests', 'basketball')])
r.json()['form']

requests库的post请求4种类型参数

用python来验证接口正确性,主要流程有:

  • 1 设置url
  • 2 设置消息头
  • 3 设置消息体
  • 4 获取响应
  • 5 解析相应
  • 6 验证数据

Content-Type的格式有四种:分别是application/x-www-form-urlencoded(这也是默认格式)、application/json、text/xml以及multipart/form-data格式。

1、application/x-www-form-urlencoded数据格式

请看代码:

?
1
2
3
4
datas = {'parameter1':'12345','parameter2':'23456'}
r = requests.post('http://example.com',data=datas)
print(r.content)
print(r.status_code)

解说:Reqeusts支持以application/x-www-form-urlencoded数据格式发送post请求,只需要将请求的参数构造成一个字典,然后传给requests.post()的data参数即可。

2、application/json数据格式 


application/json格式的请求头是指用来告诉服务端post过去的消息主体是序列化后的 JSON 字符串。

请看带代码:

?
1
2
3
4
url = 'http://www.example/post'
s = json.dumps({'key1': 'value1', 'key2': 'value2'})
r = requests.post(url, data=s)
print (r.text)

区别:

这里我们可以发现Requests模拟post请求时,请求头格式为application/x-www-form-urlencoded与application/json的主要差别在于请求主体的构造格式(前者是键值对,后者是JSON串),前者直接用字典传入,后者用json.dumps()函数将字典转为JSON串即可。

3、text/xml数据格式

请看代码:

?
1
2
3
xml = """my xml"""
headers = {'Content-Type': 'application/xml'}
requests.post('http://www.example.com', data=xml, headers=headers)

或者把xml作为一个文件来传输:

?
1
2
3
4
5
6
7
import requests
def request_ws(request):
with open(archivo_request,"r") as archivo:
    request_data = archivo.read()
target_url = "http://127.0.0.1:8000/?wsdl"
headers = {'Content-type':'text/xml'}
data_response = requests.post(target_url, data=request_data, headers=headers)

4、multipart/form-data数据格式

除了传统的application/x-www-form-urlencoded表单,我们另一个经常用到的是上传文件用的表单,这种表单的类型为multipart/form-data,multipart/form-data主要用于文件上传,当我们使用它时,必须让 form表单的enctype 等于 multipart/form-data

直接来看一个请求示例,主要:

请看代码(实现上传本地的test.txt文件):

?
1
2
3
4
import requests 
files = {"file": open("C:/Users/Administrator/Desktop/test.txt", "rb")}
r = requests.post("http://httpbin.org/post", files=files) 
print(r.text)

具体请看实际例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import requests
import json
# 设置URL
url = "http://demo.9meikf.cn/usystem/auto/getAnswer.do"
# 设置消息头
headers = {
    "Cookie":"JSESSIONID=EA01FF2B025861F39E29712C97F7DF69;CASTGC=TGT-136-bLQMf0CAikK4BGaydOfIeKd6tWpZQEznJ2ZWdcVl9ofI4LiaQb-cas01.example.org",
    "Content-Type":"application/json"
    }
# 设置消息体
data = {"companyId":"48622",
        "nodeId":6,
        "question":"不需要",
        "templateId":"c6f5ad67fc2c11e8a11800163e086942"}
# 获取相应
response=requests.post(url,headers=headers,data=json.dumps(data))
print("Status code:",response.status_code)
print(response.text)
# 解析相应
info=response.json()
# 验证数据
assert str(info['answer'])=='reject'

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/win_turn/article/details/54849734

延伸 · 阅读

精彩推荐