0

我正在尝试以下所有方法来获取推送请求信息,但它们都失败了:

@app.route('/handlepush', methods=['POST'])
def handlepush():
    """
    Handles push notifications from Foursquare
    """
    print 'handling push'
    print request

    try:
        print 'request.form:' + json.loads(request.form['checkin'],
                          parse_float=str)
    except:
        print 'request.form error'

    try:
        print 'request.data: ' + json.loads(request.data['checkin'], parse_float=str)
    except:
        print 'no request.data'

    try:
        print 'request.form' + request.form
    except:
        print 'no request.form'

    try:
        print 'request.args' + request.args
    except:
        print 'no request.args'

    if request.headers['Content-Type'] == 'text/plain':
        print'text/plain'
        return "Text Message: " + request.data

    elif request.headers['Content-Type'] == 'application/json':
        print 'application/json'
        return "JSON Message: " + json.dumps(request.json)

    try:
        print 'request.form["id"]:' + request.form['checkin']['id']
    except:
        print 'request.form error'

    try:
        print request.data['checkin']['id']
    except:
        print 'request.data error'

    try:
        print 'request.json: ' + request.json
    except:
        print 'no request.json'


    return 'hi'

这是我的回应:

2013-01-28T00:54:31+00:00 app[web.1]: <Request 'http://my.herokuapp.com/handlepush' [POST]>
2013-01-28T00:54:31+00:00 app[web.1]: handling push
2013-01-28T00:54:31+00:00 app[web.1]: no request.args
2013-01-28T00:54:31+00:00 app[web.1]: 10.71.26.114 - - [28/Jan/2013 00:54:31] "POST /handlepush HTTP/1.1" 200 -
2013-01-28T00:54:31+00:00 app[web.1]: request.form error
2013-01-28T00:54:31+00:00 app[web.1]: no request.data
2013-01-28T00:54:31+00:00 app[web.1]: no request.form
2013-01-28T00:54:31+00:00 app[web.1]: request.data error
2013-01-28T00:54:31+00:00 app[web.1]: no request.json
2013-01-28T00:54:31+00:00 app[web.1]: request.form error

所以我想知道解决这个问题的最佳方法。根据我的经验,当标头指定为“application/json”时,request.json 适用于 json POST,但我不知道如何获取此推送数据。

4

1 回答 1

1
request.form['checkin']

成功了,让我得到了 json。

于 2013-01-28T01:23:45.700 回答