python-requests 相当于 curl 的 --data-binary? [英] python-requests equivalent to curl's --data-binary?

查看:56
本文介绍了python-requests 相当于 curl 的 --data-binary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Curl 有一个选项来发送文件与 --数据二进制选项.

Curl has an option to send a file as is with the --data-binary option.

在测试 Qualys WAS API 时,以下 curl 命令有效:

When testing the Qualys WAS API, the following curl command works:

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 Client Error: Unsupported Media Type.

Using Python's requests module, I keep receiving a HTTPError: 415 Client Error: Unsupported Media Type.

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'))

尝试提交文件files参数时,也以415错误结束.

When trying to submit file files parameter, it also ended in a 415 error.

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 包中.

The reason I'm setting this up is to incorporate this into the qualysapi Python package.

推荐答案

原来我应该有的标题是

headers = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'}

这篇关于python-requests 相当于 curl 的 --data-binary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆