我正在嘗試向當前位于本地主機上的 SREST API 發送發布請求并不斷收到各種錯誤。
我目前遇到的錯誤是:
requests.exceptions.JSONDecodeError:預期值:第 1 行第 1 列(字符 0)
我正在執行的代碼:
import requests
import json
def create_bucket():
url = "http://127.0.0.1:3000/api/buckets"
headers = {
"content-type": "application/json"
}
params = {
"bucket_name": "test_bucket"
}
response = requests.post(url=url, headers=headers, params=params)
print(response.json())
create_bucket()
我究竟做錯了什么?
編輯:
嘗試response.content按要求列印,但出現錯誤:
b'<!doctype html>\n<html lang=en>\n<title>400 Bad Request</title>\n<h1>Bad Request</h1>\n<p>Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)</p>\n'
編輯2:
解決了這個問題。正確做法:
response = requests.post(url=url, headers=headers, data=json.dumps(params))
uj5u.com熱心網友回復:
根據錯誤,需要json正確編碼,嘗試:
response = requests.post(url=url, headers=headers, json=params)
請求會做正確的事
或者
response = requests.post(url=url, headers=headers, params=json.dumps(params))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/512456.html
上一篇:mongodb中的多級分組
