【问题标题】:Why did I get return code 200 instead of 201 on my post request ? (Python /Requests/Json)为什么我的发帖请求得到返回码 200 而不是 201? (Python /请求/Json)
【发布时间】:2020-05-12 23:20:45
【问题描述】:

我正在使用 pandas 和 requests 创建一个发布请求,而我创建的那个发给我一个状态码 200 而不是 201。

在这篇文章中,我从一个数据帧发送一个 JSON。这部分似乎很好。 不知道header好不好,里面改了很多东西,都没有成功。

这个问题没有显示任何错误,受请求影响的服务器也没有给出任何迹象。 第一个请求给了我访问令牌并且运行良好。

def post_json(nbr_requests):
    auth_json = {'grant_type': 'password', 'client_id': 'hidden','client_secret':'hidden','username':'hidden','password':'hidden'}
    auth_response  = requests.post('http://hidden:8080/lot/of/stufs/token',data=auth_json)
    token = auth_response.json()["access_token"]
    api_call_headers = {'content-type':'application/json', 'accept':'application/json','authorization': 'Bearer' + token}
    url_to_go = "http://localhost:8080/hidden/link"
    for i in range(nbr_requests):
        api_call_response = requests.post(url_to_go, headers=api_call_headers, json=json_array_to_send[i],data={"key": "value"})
        print (api_call_response.status_code)

【问题讨论】:

  • 您向我们展示了客户端代码,但询问了服务器。向我们展示服务器代码,即相关端点。无论如何,200 是默认的“ok”代码。如果你想返回其他任何东西,你必须明确地这样做。
  • 由于机密问题,我无法显示服务器端代码,但使用 Postman,我没有此状态代码问题,并且我得到了应有的 201。使用代码 201,它们是数据库中的插入,我至少可以看到结果。
  • 然后创建一个minimal reproducible example。如果没有看到任何代码,我们将无法帮助您。

标签: python json request http-error


【解决方案1】:

我知道这个问题不清楚,但如果有人遇到同样的问题,你应该在“Bearer”和标题中的标记之间添加一个空格。

在我上面的例子中,你应该这样做:

api_call_headers = {'content-type':'application/json', 'accept':'application/json','authorization': 'Bearer ' + token}

【讨论】:

    猜你喜欢
    • 2020-07-30
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2019-06-02
    • 2019-05-23
    • 1970-01-01
    相关资源
    最近更新 更多