我有一个服务器需要能够接受 JSON,然后处理它,然后发回 JSON。我的服务器端的代码正在使用Bottle.py 与cherrypy。所关注的路线如下:
@route ('/tagTweets', method='POST')
def tagTweets():
response.content_type = 'application/json'
# here I need to be able to parse JSON send along in this request.
为了请求此页面并测试功能,我正在使用requests模块代码:
我必须发送的数据是推文列表。数据本身是从某个返回推文列表的服务器获取的。为了获取推文,我正在使用requests.get
然后使用响应对象的json方法。这工作正常。现在我对此进行了一些处理,我必须发送这个 json,就像我获取到另一个服务器一样。
url = "http://localhost:8080/tagTweets"
data = {'sender': 'Alice', 'receiver': 'Bob', 'message': 'We did it!'}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)
我无法弄清楚如何访问随请求发送的 json。
For a application/json
POST,只需访问request.json http://bottlepy.org/docs/dev/api.html#bottle.BaseRequest.json:
@route ('/tagTweets', method='POST')
def tagTweets():
response.content_type = 'application/json'
sender = request.json['sender']
receiver = request.json['receiver']
message = request.json['message']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)