通过请求模块发送JSON,并使用bottle.py和cherrypy捕捉它[英] Sending JSON through requests module and catching it using bottle.py and cherrypy

本文是小编为大家收集整理的关于通过请求模块发送JSON,并使用bottle.py和cherrypy捕捉它的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一台服务器,需要能够接受JSON然后对其进行处理,然后将JSON发送回.我的服务器端的代码使用 bott.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.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发送请求.

推荐答案

对于application/json帖子,只需访问 :

@route ('/tagTweets', method='POST')
def tagTweets():
     response.content_type = 'application/json'
     sender = request.json['sender']
     receiver = request.json['receiver']
     message = request.json['message']

其他推荐答案

尝试一下...

//Cherrypy

import json

@route ('/tagTweets', method='POST')
def tagTweets(UpdatedData=None):
    Data = json.loads(UpdatedData)

//javascript

function SendJson()
{
    var JSONObject = { changes: [] };
    JSONObject.changes.push({"sender": "Alice", "receiver": "Bob" }
            );

    // code for IE7+, Firefox, Chrome, Opera, Safari
    if(window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();
    else// code for IE6, IE5
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            alert(xmlhttp.responseText);
        }
    }

    xmlhttp.open("POST","/tagTweets", true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(JSON.stringify(JSONObject));
}

希望这会有所帮助.

安德鲁

本文地址:https://www.itbaoku.cn/post/1975337.html

问题描述

I have a server which needs to be able to accept JSON and then process it and then send JSON back. The code at my server side is using bottle.py with cherrypy. The route in concern is the following:

@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.

For requesting this page and testing the functionality, I'm using requests module code:

The data that I have to send is list of tweets. The data is itself fetched from some server which returns list of tweets. For fetching tweets, I'm using requests.get and then using json method of response object. This is working fine. Now I after some processing on this, I have to send this json, just like I fetched to another server.

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)

I'm not able to figure out how to gain access to the json send along the request.

推荐答案

For a application/json POST, simply access request.json:

@route ('/tagTweets', method='POST')
def tagTweets():
     response.content_type = 'application/json'
     sender = request.json['sender']
     receiver = request.json['receiver']
     message = request.json['message']

其他推荐答案

Give this a try...

//cherrypy

import json

@route ('/tagTweets', method='POST')
def tagTweets(UpdatedData=None):
    Data = json.loads(UpdatedData)

//javascript

function SendJson()
{
    var JSONObject = { changes: [] };
    JSONObject.changes.push({"sender": "Alice", "receiver": "Bob" }
            );

    // code for IE7+, Firefox, Chrome, Opera, Safari
    if(window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();
    else// code for IE6, IE5
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            alert(xmlhttp.responseText);
        }
    }

    xmlhttp.open("POST","/tagTweets", true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(JSON.stringify(JSONObject));
}

Hope this helps.

Andrew