2013-06-14 2 views
0

설명서가없는 API로 작업하고 있는데 걸림돌이되었습니다.Python Request Post 발행물

def add_to_publicaster(self): 
    # function that is called in the background whenever a user signs the petition and opts in to the mailing list 
    # Makes an API call to publicaster <--- More documentation to follow ---> 
    username = app.config['PUBLICASTER_USERID'] 
    userPass = app.config['PUBLICASTER_PASS'] 
    headers = {'Authorization': {username:userPass}, "Content-type" : "application/json", "Accept":'text/plain'} 
    url = 'https://api7.publicaster.com/Rest/Subscribers.svc/1?format=json' 
    data = {"Item": { 
     "Email": "[email protected]" 
     } 
    } 
    r = requests.post(url, headers = headers, data = data) 

단순히이 형식의 POST 요청을 할 생각입니다 :

POST https://api7.publicaster.com/Rest/Subscribers.svc/1?format=json HTTP/1.1 
Content-Type: application/json 
Authorization: <AccountID>:<Password> 
Host: api7.publicaster.com 
Content-Length: 64 
Expect: 100-continue 
Connection: Keep-Alive 
{ "Item" : { 
    "Email" : [email protected] 
} 
} 

기능의 코드는, 그러나, 원하는 요청을 생성하지 않는 나는 기능을 가지고있다. 조언이 도움이 될 것입니다.

+0

무엇을 얻고 있습니까? – thefourtheye

+0

기본적으로 서버에서 요청을 처리하는 중 오류가 발생했다는 매우 긴 오류 메시지입니다. 해결책은 가능한 한 "샘플"요청을 일치시키는 것입니다. – JulianGindi

+0

당신은 json 코드로 데이터를 인코딩해야합니다 ..'json.dumps (dict (data = data))' – karthikr

답변

0

헤더와 URL에서 JSON 데이터를 게시하고 싶다고 제안했습니다.

import json 

# ... 

data = {"Item": { 
    "Email": "[email protected]" 
    } 
} 
r = requests.post(url, headers = headers, data = json.dumps(data)) 

JSON은 파이썬처럼 많이 보일 수도 있지만, 정말 자바 스크립트 소스 코드의 제한된 형태 다음 json 라이브러리를 사용하여 JSON로 파이썬 구조를 인코딩합니다.

+0

불행히도, 나는 전에 이것을 시도했지만 여전히 오류가 발생하고 있습니다. API의 문서화되지 않은 특성으로 인해 실험을 계속해야 할 수도 있습니다. – JulianGindi

+0

@JulianGindi : * * 시도한 것은 전혀 작동하지 않습니다 *. 정확한 페이로드에는 문제가있을 수 있지만 헤더가 'application/json' 데이터를 게시하고 있다고 판단되면 적어도 JSON으로 데이터를 인코딩해야합니다. –

0

인증을 올바르게 수행하지 않습니다. 함수는 다음과 같아야합니다.

def add_to_publicaster(self): 
    # function that is called in the background whenever a user signs the petition and opts in to the mailing list 
    # Makes an API call to publicaster <--- More documentation to follow ---> 
    username = app.config['PUBLICASTER_USERID'] 
    userPass = app.config['PUBLICASTER_PASS'] 
    headers = {"Content-type" : "application/json", "Accept":'text/plain'} 
    url = 'https://api7.publicaster.com/Rest/Subscribers.svc/1?format=json' 
    data = {"Item": { 
     "Email": "[email protected]" 
     } 
    } 
    r = requests.post(url, auth=(username, userPass), headers=headers, data=json.dumps(data))