如何使用Flask处理Foursquare推送POST请求?

时间:2013-01-28 00:56:54

标签: python flask foursquare

我正在尝试以下所有方法来获取push request信息并且它们都失败了:

@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 POSTs,但我不知道如何获取这个推送数据。

1 个答案:

答案 0 :(得分:1)

request.form['checkin']

做了诀窍并把我弄成了json。