如何在 POST 请求 Python 中发送原始数据
How to send raw data in a POST request Python
我要发送的正文:
update_request={
"id": "f07de0a44c2911ea8fb2bc764e10b970",
"user": {
"user": "3491574055045",
"timestamp": "1640049459",
"signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
"code": "test"
}
}
这是我现在的代码:
url = "https://api.ordergroove.com/customer/update_customer"
headers = {
'content-type': 'application/json'
}
body = """
update_request={{
"id": "f07de0a44c2911ea8fb2bc764e10b970",
"user": {
"timestamp": "1640049459",
"signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
"code": "test"
}
}}
"""
#Send and print response
response = requests.post(url, data=body, headers=headers)
如果我在 Postman 中 运行 这个虽然它工作得很好:
Postman screenshot
也许...这是一个很大的可能
url = "https://api.ordergroove.com/customer/update_customer"
data = {"update_request":{
"id": "f07de0a44c2911ea8fb2bc764e10b970",
"user": {
"timestamp": "1640049459",
"signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
"code": "test"
}
}
}
requests.post(url,json=data)
可能有用...
在您的情况下,您的 content-type 指定 JSON,这是一种常见的 body 类型,因此请求添加了一种完全不同的方式来发送 json body(使用 json 参数)通过指定 json=body。 Body 是另一种字典类型,然后为您解析为字符串并与请求一起发送。 x-www-form encoded 和 json 都是常见的 body 类型,它们本质上是字典,因此它们经常可以混淆但不能互换。
data = {
"update_request":{
"id": "f07de0a44c2911ea8fb2bc764e10b970",
"user": {
"timestamp": "1640049459",
"signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
"code": "test"
}
}
}
response = requests.post(url,json=data)#data=body for x-www-urlencoded form data, json=body for content-type json
这种情况下的请求将自动添加 content-type json header 并将字典正确格式化为字符串,而不是发送 content-type x-www-form 编码 content-type header 如果你要放 body=data
您仍然可以通过传入 json=raw_data 来为请求使用原始 json 数据,原始数据是一个字符串。这是不受欢迎的,因为如果有任何格式问题,服务器可能无法读取您的请求 body。当您可以传入一个 python 字典 object 时,没有理由这样做,如前所示,并且 requests 会为您将其解析为一个字符串!
import requests
url = "https://46463d29-e52d-4bb9-bdda-68f0dfd7d06d.mock.pstmn.io/test"
payload = " update_request={{\r\n \"id\": \"f07de0a44c2911ea8fb2bc764e10b970\",\r\n \"user\": {\r\n \"timestamp\": \"1640049459\",\r\n \"signature\": \"YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D\",\r\n \"code\": \"test\"\r\n }\r\n }}"
headers = {
'Content-Type': 'text/plain'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
您可以从邮递员本身生成代码
我要发送的正文:
update_request={
"id": "f07de0a44c2911ea8fb2bc764e10b970",
"user": {
"user": "3491574055045",
"timestamp": "1640049459",
"signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
"code": "test"
}
}
这是我现在的代码:
url = "https://api.ordergroove.com/customer/update_customer"
headers = {
'content-type': 'application/json'
}
body = """
update_request={{
"id": "f07de0a44c2911ea8fb2bc764e10b970",
"user": {
"timestamp": "1640049459",
"signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
"code": "test"
}
}}
"""
#Send and print response
response = requests.post(url, data=body, headers=headers)
如果我在 Postman 中 运行 这个虽然它工作得很好: Postman screenshot
也许...这是一个很大的可能
url = "https://api.ordergroove.com/customer/update_customer"
data = {"update_request":{
"id": "f07de0a44c2911ea8fb2bc764e10b970",
"user": {
"timestamp": "1640049459",
"signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
"code": "test"
}
}
}
requests.post(url,json=data)
可能有用...
在您的情况下,您的 content-type 指定 JSON,这是一种常见的 body 类型,因此请求添加了一种完全不同的方式来发送 json body(使用 json 参数)通过指定 json=body。 Body 是另一种字典类型,然后为您解析为字符串并与请求一起发送。 x-www-form encoded 和 json 都是常见的 body 类型,它们本质上是字典,因此它们经常可以混淆但不能互换。
data = {
"update_request":{
"id": "f07de0a44c2911ea8fb2bc764e10b970",
"user": {
"timestamp": "1640049459",
"signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
"code": "test"
}
}
}
response = requests.post(url,json=data)#data=body for x-www-urlencoded form data, json=body for content-type json
这种情况下的请求将自动添加 content-type json header 并将字典正确格式化为字符串,而不是发送 content-type x-www-form 编码 content-type header 如果你要放 body=data
您仍然可以通过传入 json=raw_data 来为请求使用原始 json 数据,原始数据是一个字符串。这是不受欢迎的,因为如果有任何格式问题,服务器可能无法读取您的请求 body。当您可以传入一个 python 字典 object 时,没有理由这样做,如前所示,并且 requests 会为您将其解析为一个字符串!
import requests
url = "https://46463d29-e52d-4bb9-bdda-68f0dfd7d06d.mock.pstmn.io/test"
payload = " update_request={{\r\n \"id\": \"f07de0a44c2911ea8fb2bc764e10b970\",\r\n \"user\": {\r\n \"timestamp\": \"1640049459\",\r\n \"signature\": \"YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D\",\r\n \"code\": \"test\"\r\n }\r\n }}"
headers = {
'Content-Type': 'text/plain'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
您可以从邮递员本身生成代码