0

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

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/")

我如何将 urlencodeddata作为帖子传递给 url?

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)。

4

1 回答 1

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"
  },
  ...
}
于 2013-05-22T21:51:20.517 回答