我試圖用python腳本來執行以下cURL命令。我想把結果保存為文本檔案。
cURL命令:
curl -d @myfile.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=xxxx"
Python腳本:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://www.googleapis.com/geolocation/v1/geolocate?key=xxxx"/span>
payload = {r "C:UsersDesktopmyfile.json"}.
res = requests.post(url, data=payload)
print(res.text)
錯誤:
{
"error": {
"code": 400,
"message": "收到無效的JSON有效載荷。未預期的token.myfile.json
^"。
"errors": [
{
"message": "收到無效的JSON有效載荷。未預期的token.myfile.json
^"。
"域"。"global",
"原因": "parseError".
}
],
"status": "INVALID_ARGUMENT"
從錯誤中,我可以理解,我提供json檔案的方式一定是錯誤的。我嘗試了不同的方法,但錯誤仍然存在。這個json檔案包含了API呼叫所需的一組引數。
請您在這里提供指導。還請指出如何將輸出保存為文本。
uj5u.com熱心網友回復:
這很接近,但你所做的只是把json 檔案名發送到遠程HTTP服務器。你需要先打開該檔案:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://www.googleapis.com/geolocation/v1/geolocate?key=xxxx"/span>
with open("C:UsersDesktopmyfile.json"/span>, 'rb') as payload:
res = requests.post(url, data=payload)
print(res.text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/319781.html
標籤:
