2015-01-04 2 views
0

파이썬에서 요청을 사용하여 json 사전을 URL에 게시하려고합니다. URL에서 문자열을 다시 가져와야하지만 코드 141 오류 ({code : 141, "error": "github 저장소 링크 누락"}이 계속 발생합니다. 요청을하려면이 웹 사이트 (http://docs.python-requests.org/en/latest/user/quickstart/)를 사용하고 있습니다.Python Requests Code 141

왜 계속 오류가 발생하는지에 대한 아이디어가 있습니까? 코드는 다음과 같습니다.

import requests 
import json 

payload = { "email" : "[email protected]", "github" : "https://github.com/"} 
headers = {'content-type': 'application/json', "Accept": 'application/json'} 
r = requests.post("http://challenge.code2040.org/api/register", params = payload, headers = headers) 

print(r.url) 
print r.text 

업데이트 : 제안은 일을하지만 지금은지고 있어요 { "코드": 141, "오류": "성공/오류가 호출되지 않은"} 내가로부터받을 응답을 저장하려고하면 오류 url을 변수에 넣은 다음 다른 url로 다시 게시하십시오.

json 인수를 도입하는 것으로 - (다시 토큰을 가지고 테스트)

r = requests.post("http://challenge.code2040.org/api/register", 
        json=payload, 
        headers=headers) 

가 :

#Store the token into a variable 
token = r.text 

payload = { "token" : token} 
headers = {'content-type': 'application/json', "Accept": 'application/json'} 
r = requests.post("http://challenge.code2040.org/api/getstring", json = payload, headers = headers) 

print r.text 

답변

0

당신이 POST 요청 및 you need to provide a JSON in the request body을하고 있기 때문에이 json argument하지 params 사용 requests 2.4.2.

+0

내가 게시하는 url은 "result1"과 "result2"라는 두 개의 키가있는 사전을 반환합니다. "result2"값만 어떻게 인쇄 할 수 있습니까? r.text를 인쇄하면 모든 값이 인쇄됩니다. – user2466886

+0

@ user2466886'r.json()'메서드를 사용하여 파이썬 사전을 얻습니다. – alecxe

+0

감사하지만 오류가 발생했습니다. { "code": 141, "error": "성공/오류가 호출되지 않았습니다"}. 아래 코드 : 토큰 = r.text 페이로드 = { "토큰"토큰} 헤더 = { '콘텐츠 유형': '응용 프로그램/JSON', "동의" '응용 프로그램/JSON'} R = requests.post ("http://challenge.code2040.org/api/getstring", json = payload, headers = headers) 인쇄 r.text – user2466886

관련 문제