3
Curl可以選擇按照--data-binary option的原樣發送文件。python-requests相當於curl的--data-binary?
當測試Qualys WAS API,下面的curl命令作品:
curl -u "username:password" -H "content-type: text/xml" -X "POST" --data-binary @- "https://qualysapi.qualys.com/qps/rest/3.0/search/was/webapp" < post.xml
post.xml:
<ServiceRequest><filters><Criteria operator="CONTAINS" field="name">PB - </Criteria></filters></ServiceRequest>
使用Python的請求模塊,我不斷收到一個HTTPError:415客戶端錯誤:不支持的媒體類型。
import requests
url = 'https://qualysapi.qualys.com/qps/rest/3.0/search/was/webapp'
payload = '<ServiceRequest><filters><Criteria operator="CONTAINS" field="name">PB - </Criteria></filters></ServiceRequest>'
headers = {'X-Requested-With': 'Python requests', 'Content-type': 'application/json'}
r = requests.post(url, data=payload, headers=headers, auth=('username', 'password'))
當試圖提交文件文件參數時,它也以415錯誤結束。
import requests
url = 'https://qualysapi.qualys.com/qps/rest/3.0/search/was/webapp'
payload = '<ServiceRequest><filters><Criteria operator="CONTAINS" field="name">PB - </Criteria></filters></ServiceRequest>'
headers = {'X-Requested-With': 'Python requests', 'Content-type': 'application/json'}
r = requests.post(url, data=payload, headers=headers, auth=('username', 'password'))
我設置這個的原因是將其合併到qualysapi Python package。
的可能重複[Python的請求 - 從文件POST數據(http://stackoverflow.com/questions/16145116/python-requests-數據後文件) –
您的問題標題頗具誤導性;你的問題在於頭文件,而不是如何用'requests'模擬'--data-binary'。我鏈接到*的確實會提出這個問題。 –
從你的'有效載荷'內容和你的'Content-type'頭部判斷,我對下面發佈的答案的直接回應是*是啊,*。 :-D –