python - 如何在Python POST请求中发送原始数据

标签 python post python-requests postman

我试图发送的正文:

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

最佳答案

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 本身生成代码

enter image description here

关于python - 如何在Python POST请求中发送原始数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70430339/

相关文章:

python - 如何强制django立即保存而不是在循环后进行批量更新

python - np.empty_like 是否有参数错误?

python - 性感:如何在 HTTP POST 中验证来自 json 数据的非字符串值

python - 由于阻塞 Queue.get() 方法导致的死锁

python - matplotlib.widgets.TextBox : change color

从 POST 获取InputStream 时出现 JAVA EOFException

post - 使用 paw-app 执行类似 POST 请求的 $.ajax

python - Python 和 XML 错误

python - 使用 Python requests 模块发出的 PUT 请求没有数据

python - 试图找到类(class)内的号码