使用 headers 和 body 请求将 HTTP POST 隐藏到 Python (Telerik Fiddle)
Covert an HTTP POST with headers and body request to Python (Telerik Fiddle)
我在 Telerik Fiddler 中使用以下内容来创建 POST 请求并成功:
POST https://login.random.com/connect/token HTTP/1.1 Connection:
Keep-Alive Content-Type: application/x-www-form-urlencoded Accept:
application/json Authorization: Basic XyXyXyXyXyXyXyXyXyXyXy=
Content-Length: 105 Host: login.random.com
grant_type=password&username=xxx&password=xxxx&scope=random.api.external
现在我试图将其转换为 Python 脚本,但它失败了(returns 错误 400):
import json
import requests
import http.client
headers = {
'Connection': 'Keep-Alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'Authorization': 'Basic XyXyXyXyXyXyXyXyXyXyXy=',
'Content-Length': '105',
'Host': 'login.random.com'
}
body = {
'grant_type': 'password',
'username': 'xxx',
'password': 'xxxx',
'scope': 'random.api.external'
}
response = requests.post('https://login.random.com/connect/token', headers=headers, data=body)
我也试过在 headers 中包含以上所有内容,但失败了(错误 400)。
我是不是错过了一个图书馆?我的脚本中的详细信息是正确的。
我找到了解决问题的方法。在 Telerik Fiddler 中,您可以下载代码(转换为 Python)插件并了解最终脚本的外观。经过修改和测试,最终脚本如下:
try:
req = urllib.request.Request("https://login.random.com/connect/token")
req.add_header("Connection", "Keep-Alive ")
req.add_header("Content-Type", "application/x-www-form-urlencoded ")
req.add_header("Accept", "application/json ")
req.add_header("Authorization", "Basic XyXyXyXyXyXyXyXyXyXyXy= ")
body = b"grant_type=password&username=xxx&password=xxxx&scope=random.api.external"
response = urllib.request.urlopen(req, body)
except urllib.error.URLError as e:
if not hasattr(e, "code"):
print('False')
response = e
except:
print('False')
print('True')
我在 Telerik Fiddler 中使用以下内容来创建 POST 请求并成功:
POST https://login.random.com/connect/token HTTP/1.1 Connection: Keep-Alive Content-Type: application/x-www-form-urlencoded Accept: application/json Authorization: Basic XyXyXyXyXyXyXyXyXyXyXy= Content-Length: 105 Host: login.random.com
grant_type=password&username=xxx&password=xxxx&scope=random.api.external
现在我试图将其转换为 Python 脚本,但它失败了(returns 错误 400):
import json
import requests
import http.client
headers = {
'Connection': 'Keep-Alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'Authorization': 'Basic XyXyXyXyXyXyXyXyXyXyXy=',
'Content-Length': '105',
'Host': 'login.random.com'
}
body = {
'grant_type': 'password',
'username': 'xxx',
'password': 'xxxx',
'scope': 'random.api.external'
}
response = requests.post('https://login.random.com/connect/token', headers=headers, data=body)
我也试过在 headers 中包含以上所有内容,但失败了(错误 400)。
我是不是错过了一个图书馆?我的脚本中的详细信息是正确的。
我找到了解决问题的方法。在 Telerik Fiddler 中,您可以下载代码(转换为 Python)插件并了解最终脚本的外观。经过修改和测试,最终脚本如下:
try:
req = urllib.request.Request("https://login.random.com/connect/token")
req.add_header("Connection", "Keep-Alive ")
req.add_header("Content-Type", "application/x-www-form-urlencoded ")
req.add_header("Accept", "application/json ")
req.add_header("Authorization", "Basic XyXyXyXyXyXyXyXyXyXyXy= ")
body = b"grant_type=password&username=xxx&password=xxxx&scope=random.api.external"
response = urllib.request.urlopen(req, body)
except urllib.error.URLError as e:
if not hasattr(e, "code"):
print('False')
response = e
except:
print('False')
print('True')