Python 请求 body 与我输入的 body 不匹配
Python Requests body doesn't match what I put as the body
标题可能没有意义,但我的问题是我在请求中设置了一些东西作为 body 但是当我打印出来时它没有打印出实际的 body 是什么.
这是我的代码:
data = {
"offers": [
{
"userId": 435771547,
"userAssetIds": [
2409285794
],
"robux": 0
},
{
"userId": userId,
"userAssetIds": [
1380767576
],
"robux": 0
}
]
}
requested = requests.post("https://trades.roblox.com/v1/trades/send", data = data, cookies = cookie, headers = {
"Content-Type": "application/json",
"X-CSRF-TOKEN": token
})
print(requested.request.body)
请求 returns 来自 requests.post()
的响应,而不是请求本身。所以通常你会写类似 respone = requests.post(...)
的东西然后说检查 response.status_code
,然后读 response.json()
,等等
为了调试目的,为了查看请求本身,您可以使用更底层的方法,使用 requests.Request()
来构造请求,然后打印它。您可以在 this answer.
中找到一个很好的例子
另一种选择是分析正在记录的请求。请参阅示例 here。
顺便说一下,如果您想发送 json 正文,请使用 json
关键字参数而不是 data
。
requests.post('https://httpbin.org/post',
json={'a': 'hello', 'b': [1, 2, 3]})
标题可能没有意义,但我的问题是我在请求中设置了一些东西作为 body 但是当我打印出来时它没有打印出实际的 body 是什么.
这是我的代码:
data = {
"offers": [
{
"userId": 435771547,
"userAssetIds": [
2409285794
],
"robux": 0
},
{
"userId": userId,
"userAssetIds": [
1380767576
],
"robux": 0
}
]
}
requested = requests.post("https://trades.roblox.com/v1/trades/send", data = data, cookies = cookie, headers = {
"Content-Type": "application/json",
"X-CSRF-TOKEN": token
})
print(requested.request.body)
请求 returns 来自 requests.post()
的响应,而不是请求本身。所以通常你会写类似 respone = requests.post(...)
的东西然后说检查 response.status_code
,然后读 response.json()
,等等
为了调试目的,为了查看请求本身,您可以使用更底层的方法,使用 requests.Request()
来构造请求,然后打印它。您可以在 this answer.
另一种选择是分析正在记录的请求。请参阅示例 here。
顺便说一下,如果您想发送 json 正文,请使用 json
关键字参数而不是 data
。
requests.post('https://httpbin.org/post',
json={'a': 'hello', 'b': [1, 2, 3]})