【问题标题】:Python Post call throwing 400 Bad Request using requests libraryPython Post调用使用请求库抛出400 Bad Request
【发布时间】:2019-09-06 19:52:00
【问题描述】:

我正在编写一个将 POST 到端点的 python 脚本,但由于某种原因,我收到 400 Bad Request。我不确定我在这里缺少什么。我的代码如下:更新代码以包含引用者,但我得到了同样的错误。我保持参考 URL 与帖子 URL 相同

import requests
import csv
import simplejson
import json
url = 'https://xyz.io/v1/candidates'
headers = {'content-type': 'application/json',
       'On-Behalf-Of':'{334401}',
       'Authorization':'Ommited'
       'referer': 'https://xyz.io/v1/candidates'
       }
data={
  "first_name": "IamTest",
  "last_name": "IamTest"
}
response = requests.post(url,data=data,headers=headers)
print(response)
print(response.reason)

【问题讨论】:

    标签: python-3.x post python-requests


    【解决方案1】:

    如果您没有来源和引用标头,大多数网站都会拒绝该请求,还可以使用 chrome 开发人员工具,并在点击登录按钮时查看发布请求并将发布请求发送到该 URL。如果登录凭据不正确,一些网站还会发送400 响应代码。

    import requests
    import csv
    import simplejson
    import json
    
    
    url = 'https://xyz.io/v1/candidates'
    
    headers = {
           'content-type': 'application/json',
           'On-Behalf-Of':'{334401}',
           'Authorization':'Ommited',
           'referer': 'https://xyz.io/v1/candidates',
           'origin' : 'homepage of the site', #ex: 'https://www.instagram.com'
           'referer' : 'login page of the site'
           }
    data = {
          "first_name": "IamTest",
          "last_name": "IamTest"
    }
    
    response = requests.post(url,data=data,headers=headers)
    
    print(response)
    print(response.reason)
    

    【讨论】:

    • 感谢您的回答,但我是 python 的新手,所以我实际上并没有明白您想说什么。你能用正确的语法重新发布我的代码吗
    • @AshishVerma 回答了你的问题
    猜你喜欢
    • 2023-03-21
    • 2020-05-29
    • 2013-10-02
    • 1970-01-01
    • 2019-10-22
    • 2016-05-18
    • 1970-01-01
    • 2021-12-02
    • 2020-03-19
    相关资源
    最近更新 更多