无法使用请求库python发出请求

时间:2014-04-17 17:54:04

标签: python

我正在尝试使用python中的请求模块与休息服务器进行通信

这就是我想要做的事情:

headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
data = {'foo':'foo','bar':'bar'}
r = requests.post("http://localhost:8080/foo",headers=headers,data=json.dumps(data))
print r.text
u'Resource representation is only available with these Content-Types:\napplication/json; charset=UTF-8'

我该如何解决这个问题? 感谢

1 个答案:

答案 0 :(得分:1)

尝试将标题更改为:

headers = {'Accept': 'application/json; charset=UTF-8'}

upd :我认为最正确的标题是:

headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
相关问题