我尝试向服务器发出发布请求并在 postman 中获得回复。它的工作正常。但是当我在 python 中尝试这个时,服务器发送“无效的客户端”作为回复。在 postman 中,这件事工作正常。这是我的Python代码
import mysql.connector
import requests
import json
import urllib3
import time
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password = "",
database = "python-db"
)
mycursor = mydb.cursor()
url = "https://example.com/oauth2/token"
header = {
"Content-Type":"application/x-www-form-urlencoded",
"Content-Length":"121",
"Host":"example.com",
"Connection":"Keep-Alive",
"Accept-Encoding":"gzip",
"User-Agent":"okhttp/3.11.0"
}
payload = {b"scope=scope1&password=123&client_id=789&username=951&grant_type=password"}
response = requests.post(url, data=(payload), headers=header, verify=False)
r_json = response.json()
print(r_json)
我在 postman 中尝试使用这些标题和数据,它工作正常。在 postman 有效负载中,数据类型是原始的。错误是什么?我该如何解决这个问题?
最佳答案
payload ="scope=scope1&password=123&client_id=789&username=951&grant_type=password"
response = requests.post(url, data=payload, headers=header, verify=False)
关于python - 如何使用 application/x-www-form-urlencoded 在 python 中发出原始数据发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63080510/