python发送json请求_使用Python请求发布JSON
我需要將JSON從客戶端發(fā)布到服務(wù)器。我正在使用Python 2.7.1和simplejson。客戶端正在使用請(qǐng)求。服務(wù)器是CherryPy。我可以從服務(wù)器獲取硬編碼的JSON(代碼未顯示),但是當(dāng)我嘗試將JSON POST到服務(wù)器時(shí),會(huì)收到“ 400 Bad Request”。
這是我的客戶代碼:
data = {'sender': 'Alice',
'receiver': 'Bob',
'message': 'We did it!'}
data_json = simplejson.dumps(data)
payload = {'json_payload': data_json}
r = requests.post("http://localhost:8080", data=payload)
這是服務(wù)器代碼。
class Root(object):
def __init__(self, content):
self.content = content
print self.content # this works
exposed = True
def GET(self):
cherrypy.response.headers['Content-Type'] = 'application/json'
return simplejson.dumps(self.content)
def POST(self):
self.content = simplejson.loads(cherrypy.request.body.read())
有任何想法嗎?
總結(jié)
以上是生活随笔為你收集整理的python发送json请求_使用Python请求发布JSON的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 男子高烧近41度 体温计贴脸狂飙:近期谨
- 下一篇: 传音即将在本月发布一款入门新机 背壳设计