2014-09-19 3 views
0

ReST API를 테스트하기 위해 요청 라이브러리를 사용하고 있습니다. 라이브러리 호출을 요청하려면 아래의 cURL을 trasform하려고 할 때 문제가 있습니다.python의 Request 라이브러리를 사용하여 첨부 파일 및 매개 변수를 사용하여 API 호출을 수행하는 방법

https://upload.box.com/api/2.0/files/content \ -H "인증 : 무기명 access_token이"\ -F 파일 이름 = @ FILE_NAME \ -F PARENT_ID = PARENT_FOLDER_ID

나는이 포럼에서 많은 제안을 시도했다. 그러나 아무 것도 효과가 없었습니다.

내가 코멘트 후 addedd 코드는 다음과 같습니다

내가 쓴 코드이었다

def upload_a_file(url, folder_id, file_name, access_token): field_values의 = "{\ '파일 이름 \'(파일 이름, 개방 ("+ FILE_NAME + ", \ 'RB의 \'))} " payload = "{\'parent_id\':"+folder_id+"}" request_headers = { '인증': '무기명'+ access_token은} result = requests.post(url, headers=request_headers, data=payload, files=field_values) 응답 = result.json() print response

+0

페이로드 등을 문자열로 저장하는 이유는 무엇입니까? 나의 원래보기를보십시오; 요청은 사전을 기다리고 있습니다. http://docs.python-requests.org/ko/latest/user/quickstart/#more-complicated-post-requests – tufelkinder

답변

1

난 당신이 requests 라이브러리를 의미하는 것 같은데요?

그렇다면 여기에 내가 어떻게하는지 나와 있습니다.

access_token = <user access token> 
filename = <name of the file as you want it to appear on Box> 
src_file = the actual file path 
parent_id = the id of the folder you want to upload to 

headers = { 'Authorization' : 'Bearer {0}'.format(access_token) } 
url = 'https://upload.box.com/api/2.0/files/content' 
files = { 'filename': (filename, open(src_file,'rb')) } 
data = { "parent_id": parent_id } 
response = requests.post(url, data=data, files=files, headers=headers) 
file_info = response.json() 
+0

작동하지 않음 :(. –

+0

더 자세한 정보를 제공 할 수 있습니까? 요청 패키지 - 앱을 인증 했습니까? 변수를 명확히하기 위해 위의 내용을 수정합니다. – tufelkinder

관련 문제