简体   繁体   English

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

[英]Why did I get return code 200 instead of 201 on my post request ? (Python /Requests/Json)

I am using pandas and requests to create a post request, and the one I am creating sent me back a status code 200 instead of a 201.我正在使用 Pandas 和 requests 创建一个 post 请求,而我正在创建的那个向我发送了一个状态代码 200 而不是 201。

In this post, I send a JSON from a dataframe.在这篇文章中,我从数据帧发送了一个 JSON。 This part seems to be good.这部分似乎不错。 I don't know if the header is good or not, I changed a lot of things in it, without success.不知道header好不好,里面改了很多东西,都没有成功。

This problem doesn't show any error and the server affected by the request doesn't give any sign too.这个问题没有显示任何错误,受请求影响的服务器也没有给出任何迹象。 The first request give me the access token and work well.第一个请求给了我访问令牌并且运行良好。

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)

I know the question wasn't clear, but if someone got the same problem, you should add a space between "Bearer" and the token in the header.我知道这个问题不清楚,但是如果有人遇到同样的问题,您应该在“Bearer”和标题中的令牌之间添加一个空格。

In my example above you should do something like this :在我上面的例子中,你应该做这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM