用Python的POST测试RESTful API

|| 我正在尝试测试我正在使用的RESTful接口(我正在使用该接口:codeigniter-restserver),并且我想使用Python。 ѭ0似乎工作正常,但我对ѭ1遇到麻烦。我不是在问这个库的来龙去脉,而是在尝试 弄清楚如何用Python测试ѭ1。这是我所拥有的:
import httplib, urllib

params = urllib.urlencode({
    \'sentence\':     \'esta es una frase\',
    \'translation\':  \'this is a sentence\'
})

headers = {
    \"Content-type\": \"application/x-www-form-urlencoded\", 
    \"Accept\":       \"text/plain\"
}

conn = httplib.HTTPConnection(\"localhost:80\")
conn.request(\"POST\", \"/myapp/phrase\", params, headers)

response = conn.getresponse()
print response.status, response.reason

data = response.read()
conn.close()
此脚本足以作为测试“ 1”的方法吗?我在SO上看到了很多关于人们寻找GUI工具来执行此操作的要求(Firefox插件等) 但是对我而言,首先构建RESTful应用程序的重点是拥有一个我可以编写脚本的API以快速修改数据库。 (用数据填充 从JSON文件中获取)。 使用这种基于Python的方法是否正确? 谢谢     
已邀请:
        POST通常通过更高级别的函数urllib2完成。
headers = {\'User-Agent\': user_agent}

data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
    
        直接写到ѭ6很好,但是级别较低。 查看“请求”模块。这是一种非常简单而Python化的方法来设计和发送http请求。
import requests

requests.post(url, data={}, headers={}, files={}, cookies=None, auth=None)
    
        还有Nap,它只是请求的包装,但可以方便地调用例如HTTP API。 用法示例:
from nap.url import Url
api = Url(\'http://httpbin.org/\')

response = api.post(\'post\', data={\'test\': \'Test POST\'})
print(response.json())
更多示例:https://github.com/kimmobrunfeldt/nap#examples 免责声明:我写午睡。     

要回复问题请先登录注册