2015-01-31 3 views
1

을 업로드하지 않습니다.파이썬 요청은 파이썬의 요청이 curl 명령을 재현하기 위해 노력하고 파일

import requests 

file = {'test.gpx': open('test.gpx', 'rb')} 

payload = {'app_id': 'my_id', 'app_key': 'my_key'} 
headers = {'Content-Type':'application/gpx+xml', 'Accept':'application/json'} 


r = requests.post("https://test.roadmatching.com/rest/mapmatch/", files=file, headers=headers, params=payload) 

그리고 오류 얻을 : 지금은 파이썬을 시도

<Response [400]> 
{u'messages': [], u'error': u'Invalid GPX format'} 

내가 잘못하고있는 중이 야 무엇을? 어딘가에 data-binary을 지정해야합니까? https://mapmatching.3scale.net/mmswag

답변

3

컬이 POST 본문 자체로 파일을 업로드,하지만 당신은 다중/폼 데이터를 본체로 인코딩 requests을 요구하고있다 :

API는 여기에 설명되어 있습니다. 여기 files를 사용하지 마십시오 data 인수로 파일 객체를 전달 :

import requests 

file = open('test.gpx', 'rb') 

payload = {'app_id': 'my_id', 'app_key': 'my_key'} 
headers = {'Content-Type':'application/gpx+xml', 'Accept':'application/json'} 

r = requests.post(
    "https://test.roadmatching.com/rest/mapmatch/", 
    data=file, headers=headers, params=payload) 

을 당신이 당신을 위해 폐쇄 될거야 with 문에서 파일을 사용하는 경우 업로드 후 :

payload = {'app_id': 'my_id', 'app_key': 'my_key'} 
headers = {'Content-Type':'application/gpx+xml', 'Accept':'application/json'} 

with open('test.gpx', 'rb') as file: 
    r = requests.post(
     "https://test.roadmatching.com/rest/mapmatch/", 
     data=file, headers=headers, params=payload) 

curl documentation for --data-binary :

(HTTP) This posts data exactly as specified with no extra processing whatsoever.

If you start the data with the letter @ , the rest should be a filename. Data is posted in a similar manner as --data-ascii does, except that newlines and carriage returns are preserved and conversions are never done.

관련 문제