2011-03-22 3 views
2

httplib2 Http 클래스를 사용하여 Twilio에 대한 API 요청을 만들려고했는데 요청을 설정하는 방법에 상관없이 내 게시물 데이터를 보내지 않습니다. 로컬 URL에 게시하고 게시물 인수가 비어 있기 때문에 이것을 알고 있습니다. 여기 내 코드입니다 :Python httplib2.Http 게시 매개 변수를 보내지 않습니다.

_TWILIO_URL = 'https://api.twilio.com/2010-04-01/Accounts/%s/%s' 
class Api(object): 
    ''' 
    A Python interface to the Twilio API 
    ''' 

    def __init__(self, AccountSid, AuthToken, From): 
     self.AccountSid = AccountSid 
     self.AuthToken = AuthToken 
     self.From = From 

    def _get_from(self, From): 
     """Use the provided From number or the one defined on initialization""" 
     if From: 
      return From 
     else: 
      return self.From 

    def call(self, To, Url, From=None): 
     """Sends a request to Twilio having it call a number; the provided URL will indicate how to handle the call""" 
     url = _TWILIO_URL % (self.AccountSid, 'Calls') 
     data = dict(From=self._get_from(From), To=To, Url=Url) 
     return self.request(url, body=urlencode(data)) 

    def request(self, url, method='POST', body=None, headers={'content-type':'text/plain'}): 
     """Send the actual request""" 
     h = Http() 
     h.add_credentials(self.AccountSid, self.AuthToken) 
     resp, content = h.request(url, method=method, body=body, headers=headers) 

     print content 

     if resp['status'] == '200': 
      return loads(content) 
     else: 
      raise TwilioError(resp['status'], content) 

    def sms(self, To, Body, From=None): 
     """Sends a request to Twilio having it call a number; the provided URL will indicate how to handle the call""" 
     url = _TWILIO_URL % (self.AccountSid, 'SMS/Messages') 
     data = dict(From=self._get_from(From), To=To, Body=Body) 
     return self.request(url, body=urlencode(data)) 

나는 문제 해결에 대해 이야기 구글에 아무것도 찾을 수 없습니다

답변

0

그것은 '콘텐츠 유형'을 application/x-www-form '으로 설정해야합니다 밝혀 -urlencoded '. 아무도 이유를 아는 경우 알려 주시기 바랍니다.

+0

콘텐츠 유형이 IS이기 때문입니다. POST 요청 (HTML 양식에서 사용될 때)에 대한 가장 분명한 내용 유형이긴하지만 POST는 html, xml, pdf 등과 같은 다른 내용에도 사용될 수 있으며 심지어 html 양식은 이 유형 (http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4 참조). 이 방법을보십시오 : 당신은 "파일"을 올리며 어떤 유형인지 선언해야합니다. – Steven

3

Twilio는 언급 their API docs concerning POST requests이 요구 사항 :

그러나
에 HTTP 의 Content-Type 헤더를 설정해야합니다 "응용 프로그램/x-www-form-urlencoded를"당신이 경우 요청을 을 자신의 클라이언트를 작성하십시오.

관련 문제