簡體   English   中英

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

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

我正在使用 Pandas 和 requests 創建一個 post 請求,而我正在創建的那個向我發送了一個狀態代碼 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)

我知道這個問題不清楚,但是如果有人遇到同樣的問題,您應該在“Bearer”和標題中的令牌之間添加一個空格。

在我上面的例子中,你應該做這樣的事情:

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