为什么 Python 请求会导致 HTTP 400 错误,但在 Postman 和 Curl 中发布却不会?

问题描述 投票:0回答:0

我看不出我做错了什么。我可以从 Python 成功发布到 HTTPBIN,使用 Curl 发布到我的网站,我可以使用 Postman 发布到我的 Wordpress 网站。 HTTPBIN 看起来像这样:

200
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "password": "1234 abcd 5678 edfg 9012 hijk", 
    "username": "johnnyjohnboy"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "61", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.28.2", 
    "X-Amzn-Trace-Id": "Root=1-234234234-234234234234234234"
  }, 
  "json": null, 
  "origin": "12.12.12.121", 
  "url": "https://httpbin.org/post"
}

使用这个 curl 命令我也成功了:

curl -H "Content-type: multipart/form-data" \
     -F username=johnnyjohnboy \
     -F password="1234 abcd 5678 edfg 9012 hijk" \
     -X POST \
     https://www.mywebsite.co.uk/wp-json/jwt-auth/v1/token

我不能做的是从 Python 发布到我的网站,

这是我正在使用的:

resp = requests.post('https://www.mywebsite.co.uk/wp-json/jwt-auth/v1/token', data={"username": "johnnyjohnboy", "password": "1234 abcd 5678 edfg 9012 hijk"})
print(resp.status_code)
print(resp.text)


# 400
# 400 - Bad Request | Your browser sent a request this server could not understand.

我可以在 Postman 中看到它是对相同 URL 和

application/x-www-form-urlencoded
的 POST 请求,它看起来像
username=johnnyjohnboy&password=1234+abcd+5678+edfg+9012+hijk
.

代码甚至在几天前才运行,从那以后,什么都没有..它是一个 Wordpress 站点,所以可以以某种方式自动更新,但我看不出当 Postman 仍然工作时这会有什么不同。

我真的看不到树上的木头..

wordpress python-requests wordpress-rest-api
© www.soinside.com 2019 - 2024. All rights reserved.