我正在嘗試使用 Python 發布一個簡單的 POST 請求,但收到意外的錯誤消息。文檔示例如下:GET /configurations :獲取配置列表參數:token(字符串)POST /configurations :發布新配置參數:token(字符串)、configName(字符串)、configParameters(數組)如果我嘗試下面的代碼,GET 請求就可以正常工作:parameters = 'token=' + tokenurl = base_url + '/configurations'response = requests.get(url, params=parameters)print(response.json())但是,如果我嘗試使用下面的代碼來處理 POST 請求,則會收到錯誤 401:“無效令牌”configName = 'test_create_config' + str(random.randint(0, 1000000))configParameters = [ {'parameter': 1, 'parameterValue': '1'}, {'parameter': 2, 'parameterValue': '0'} ]body = { 'token': token, 'configName': configName, 'configParameters': configParameters }url = base_url + '/configurations'response = requests.post(url, data=body)print(response.json())我確信我遺漏了一些東西,但我找不到什么,因為令牌與用于獲取請求的令牌相同。
1 回答

搖曳的薔薇
TA貢獻1793條經驗 獲得超6個贊
根據您發布的 Swagger 文檔,您傳遞的參數應該作為 URL 查詢參數 - 而不是正文。
- in: query name: token description: Your token value. required: true type: string
所以你應該將請求更改為:
response = requests.post(url, params=body) # params are query parameters
添加回答
舉報
0/150
提交
取消