Error processing Json request

I am sending a json request and capturing the request parameter in python flask service to process further. However, when I json dump the request and try to gather specific parameter values from it, it gives me error

"string indices must be integers, not str"

This is the json that I parsed in python flask:

{"description": "URL: https://sometest.xyz.com/test16/apicalln Issue: sample issuen Result: failn Severity: lown Category: sample category", "issuetype": {"name": "Bug"}, "priority": {"name": "Trivial"}, "project": "sample-project", "summary": "sample issue - https://sometest.xyz.com/test16/apicall"}

I am not able to figure out what is wrong I'm doing.

Here is my python flask code:

    from flask import Flask
    from flask import request
    from flask import json
    import httplib
    httplib.HTTPConnection.debuglevel = 2

    app = Flask(__name__)

    @app.route('/', methods=['POST','GET'])
    def main():
        _jsonreq = json.dumps(request.json)
        return _jsonreq['project']

    if __name__ == "__main__":
        app.run(debug=True)

You are assigning a JSON string to _jsonreq , but then trying to use it like a dictionary. Try changing your code to:

@app.route('/', methods=['POST','GET'])
def main():
    _jsonreq = request.json
    return _jsonreq['project']
链接地址: http://www.djcxy.com/p/48564.html

上一篇: Flask POSTs with Trailing Slash

下一篇: 处理Json请求时出错