如何使用 Python 请求设置媒体类型?
How to set media type with Python requests?
我正在尝试使用 Python 个请求重现此 curl 语句:
curl -T data/Graph.obj -X POST localhost:8080
我的 Python 代码如下:
files = {'Graph.obj': open('data/Graph.obj', 'rb')}
r = requests.post('http://localhost:8080', files=files)
curl 语句工作正常。但是对于 Python 代码,我得到错误 HTTP 415 Unsupported Media Type HTTP
.
如何正确设置媒体类型?或者我还缺少什么?
你可以这样做:
url = 'http://httpbin.org/post'
files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
我正在尝试使用 Python 个请求重现此 curl 语句:
curl -T data/Graph.obj -X POST localhost:8080
我的 Python 代码如下:
files = {'Graph.obj': open('data/Graph.obj', 'rb')}
r = requests.post('http://localhost:8080', files=files)
curl 语句工作正常。但是对于 Python 代码,我得到错误 HTTP 415 Unsupported Media Type HTTP
.
如何正确设置媒体类型?或者我还缺少什么?
你可以这样做:
url = 'http://httpbin.org/post'
files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file