POST请求在Postman中有效,但在Python Requests中无效(带有机器人检测功能的200个响应) [英] POST request works in Postman, but not in Python Requests (200 response with robot detection)
问题描述
我有一个POST请求,可与两个Postman和cURL完美配合(它返回数据的JSON Blob).但是,当我使用Python的Requests库执行完全相同的请求时,会收到200成功响应,但是得到的不是我的JSON Blob,而是:
I have a POST request that works perfectly with both Postman an cURL (it returns a JSON blob of data). However, when I perform the exact same request with Python's Requests library, I get a 200 success response, but instead of my JSON blob, I get this:
<html>
<head>
<META NAME="robots" CONTENT="noindex,nofollow">
<script src="/_Incapsula_Resource?SWJIYLWA=5074a744e2e3d891814e9a2dace20bd4,719d34d31c8e3a6e6fffd425f7e032f3">
</script>
<body>
</body></html>
我已经使用HTTP请求容器来验证Postman/cURL的请求与Python请求中的请求完全相同.
I've used HTTP request bins to verify that the request from Postman/cURL is exactly the same as the one from Python Requests.
这是我在cURL中的邮递员请求:
Here is my Postman request in cURL:
curl -X POST \
https:/someurl/bla/bla \
-H 'Content-Type: application/json' \
-H 'Postman-Token: 2488e914-531e-4ac7-ae8d-8490b2242396' \
-H 'Referer: https://www.host.com/bla/bla/' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:65.0) Gecko/20100101 Firefox/65.0' \
-H 'cache-control: no-cache' \
-d '{"json1":"blabla","etc":"etc"}'
...这是我的Python代码:
...and here is my Python code:
payload = {
"json1": "blabla",
"etc": "etc",
}
headers = {
'Host': 'www.host.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://www.host.com/bla/bla/',
'Content-Type':'application/json',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
'Origin': 'https://www.host.com',
}
s = requests.Session()
response_raw = s.post(url, json=payload, headers=headers)
print(response_raw)
print(response_raw.text)
我已经验证了有效载荷和标头是正确和有效的.任何帮助将非常感激;谢谢!
I have verified that the payload and headers are correct and valid. Any help would be much appreciated; thanks!
推荐答案
您将收到200个成功响应,但响应中没有JSON数据.
这意味着这只是一个响应对象.它仅包含响应代码
从响应中提取Blob信息,将响应对象转换为json
只需json_resp = response_raw.json()
json_resp
包含您的实际响应详细信息.
You are getting a 200 success response but not JSON data in the response.
This means that is just a response object. It contains only response code
to extract blob information from the response, convert response object to json
simply json_resp = response_raw.json()
This json_resp
contains your actual response details.
这篇关于POST请求在Postman中有效,但在Python Requests中无效(带有机器人检测功能的200个响应)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!