为什么我在发帖请求中得到返回码200而不是201? (Python / Requests / Json)

问题描述 投票:-2回答:1

我正在使用熊猫和请求来创建发布请求,而我正在创建的发送给我的状态码是200,而不是201。

在本文中,我从数据帧发送JSON。这部分似乎很好。我不知道标题是否好,我更改了其中的许多内容,但均未成功。

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

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)
python json request http-error
1个回答
0
投票

我知道这个问题尚不清楚,但是如果有人遇到相同的问题,则应在“ Bearer”和标头中的令牌之间添加一个空格。

在上面的示例中,您应该执行以下操作:

api_call_headers = {'content-type':'application/json', 'accept':'application/json','authorization': 'Bearer ' + token}
© www.soinside.com 2019 - 2024. All rights reserved.