[英]Rewriting a curl post call with file upload in Python as request
我有这个 curl 调用到本地graphhopper服务器,它可以工作:
curl -XPOST -H "Content-Type: application/gpx+xml" -d @home/jd/test1.gpx "localhost:8989/match?vehicle=car&type=json"
我现在想在 python 中自动化这个调用,到目前为止得到了以下结果:
import requests
url = 'http://localhost:8989/match'
headers = {'Content-Type': "application/gpx+xml"}
files = {'upload_file': open('home/jd/test1.gpx','rb')}
values = {'vehicle': 'car', 'type': 'json'}
r = requests.post(url, files=files, data=values, headers=headers)
print (r.status_code)
print (r.raise_for_status())
我收到了对 URL http://localhost:8989/match 的错误请求
我在这里想念什么?
好的,我找到了解决方案:
由于 Curl 的 Post 请求上传了一个文件,因此我必须将文件 objekt 直接传递到数据中。 我确实将其他参数放回了 URL。
import requests
url = 'http://localhost:8989/match?vehicle=car&type=json'
headers = {'Content-Type': 'application/gpx+xml'}
r = requests.post(url, data=open('/home/jd/test1.gpx','rb'), headers= headers)
print (r.status_code)
print (r.raise_for_status())
print (r.text)
奇迹般有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.