python - 如何使用 application/x-www-form-urlencoded 在 python 中发出原始数据发布请求

标签 python python-3.x python-requests http-headers postman

我尝试向服务器发出发布请求并在 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/

相关文章:

python-3.x - PyQt5 QWebEngineView 无法正常工作

python - 在 python 中使用 requests 发布到 html 表单?

Python 请求抛出 SSL 错误

python - 如何更改 Powershell 中的路径?

python - 在 Django 1.8 的 Mysql 中使用自定义用户数据库结构

python - 无法同时抓取字符串和列表

python - 使用 python requests 库网站总是挂起

python - Python 3 : unsupported operand type(s) for -: 'str' and 'float'

python - 在 mac os 和 python 中设置 SIGKILL 处理程序时出现 OSError

python - 如何在Python中打开的txt文件中添加新行?