将帖子数据传递给python api以进行twilio电话呼叫

时间:2013-05-22 21:32:33

标签: django post twilio

我想将一些POST数据发送到url,一旦在twilio调用中建立连接,就会调用它们。这是我的代码:

import urllib, urllib2
from twilio.rest import TwilioRestClient

account = "xxx"
token = "xxx"
client = TwilioRestClient(account, token)

server_url = "http://ec2-xx.xx.xx.compute-1.amazonaws.com/"
values = dict(name='mytime', \
              appt_time='2:30 PM', \
              location='Arizona Location', \
              client = "Suwanee",
    )
data = urllib.urlencode(values)
req = urllib2.Request(server_url, data)

call = client.calls.create(to="123456789", 
                           from_="987654321", 
                           url="ec2-xx.xx.xx.compute-1.amazonaws.com/hello/")

如何将urlencoded data作为帖子传递给网址?

ec2-xx.xx.xx.compute-1.amazonaws.com正在运行django,当我发送以下命令时,此服务器能够查看发布数据:

curl -X POST -d "client=mytime+Suwanee&time=2%3A30+PM&location=Suwanee+Location&name=mytime2" "http://127.0.0.1:8000/remind/"

如何在一开始提供的代码段中复制相同的行为?我只想使用POST(不是GET)。

1 个答案:

答案 0 :(得分:0)

为了您的功能,我建议您使用requests库。

使用该库发出POST请求的示例:

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)
>>> print r.text
{
  ...
  "form": {
    "key2": "value2",
    "key1": "value1"
  },
  ...
}