【问题标题】:How to send raw data in a POST request Python如何在 POST 请求 Python 中发送原始数据
【发布时间】:2022-01-22 13:45:37
【问题描述】:

我要发送的正文:

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

【问题讨论】:

  • 您在此处发布的代码有什么问题...?
  • 另外,他们接受非标准 JSON 作为有效的 POST 正文是什么类型的 API?
  • 这似乎不适合发布...

标签: python post python-requests postman


【解决方案1】:
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)

您可以生成邮递员本身的代码

【讨论】:

  • 谢谢!!!!!!!!!这正是我所需要的,我不知道我可以直接从 Postman 获得代码 sn-p。有效载荷结构非常不稳定,我不知道如何构建它。不过这效果很好。
【解决方案2】:

也许……这是一个很大的可能

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)

可能有用...

【讨论】:

    【解决方案3】:

    在您的情况下,您的内容类型指定 JSON,这是一种常见的正文类型,因此请求添加了一种完全不同的方式来通过指定 json=body 来发送 json 正文(使用 json 参数)。 Body 是另一种字典类型,然后会为您解析为字符串并随请求一起发送。 x-www-form 编码和 json 都是常见的正文类型,本质上是字典,因此它们可以经常混淆但不能互换。

    
    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 标头并将字典正确格式化为字符串,而不是发送 content-type x-www-form 编码的 content-type 标头,如果您要放置 body =数据

    您仍然可以通过传入 json=raw_data 将原始 json 数据用于请求,原始数据是一个字符串。这是不受欢迎的,因为如果存在任何格式问题,服务器可能无法读取您的请求正文。当您可以像前面所示的那样传入一个 python 字典对象并请求将其解析为您的字符串时,没有理由这样做!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 2023-03-03
      相关资源
      最近更新 更多