[英]Post request using Python urllib
我正在使用python3 urllib向api發送請求。 這是我的代碼,
headers = {}
headers['Content-Type']='application/x-www-form-urlencoded; charset=UTF-8'
#headers['X-Csrf-Token']= {'mode: cors'}
req = urllib.request.Request(url=URL, headers=headers, method='POST')
res = urllib.request.urlopen(req)
print(res.read())
我得到以下回應,
urllib.error.HTTPError: HTTP Error 415: Unsupported Media Type
API端點運行正常,我已經用Postman對其進行了測試。 這是完成工作的等效javascript代碼,
return fetch(url, { credentials : 'include', method: 'post'})
因此,我假設我必須找到一種將憑證添加到標頭的方法。 我可以在Python中包含並行憑證嗎?還是必須獲取特定的cookie並在請求中進行設置。 任何幫助表示贊賞。
錯誤415表示“內容類型”存在問題。 “ application / x-www-form-urlencoded”沒有任何參數。
https://www.w3.org/TR/html5/iana.html#application/x-www-form-urlencoded
關於fetch()的“憑據”,您將需要具有維護會話的能力(根據需要發送cookie)。 使用請求庫的Session會容易得多
http://docs.python-requests.org/en/master/user/advanced/#session-objects
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.