Flask JSON request is None

18,917

First of all request.get_json(force=True) returns an object (or None if silent=True). jsonify converts objects to JSON strings. You're trying to access str_val.flask_id. It's impossible. However, even after removing redundant jsonify call, you'll have to change result.flask_id to result['flask_id'].

So, eventually the code should look like this:

@app.route('/transaction_result/', methods=['POST'])
def transaction_result():
    result = request.get_json()
    return result['flask_id']

And you are absolutely right when you're using REST client to test the route. It crucially simplifies testing process by reducing involved parts. One well-known problem during sending requests from a flask app to the same app is running this app under development server with only one thread. In such case a request will always be blocked by an internal request because the current thread is serving the outermost request and cannot handle the internal one. However, since you are sending a request from the Celery task, it's not likely your scenario.

UPD: Finally, the last one reason was an IP address 0.0.0.0. Changing it to the real one solved the problem.

Share:
18,917
Jonathan Stegall
Author by

Jonathan Stegall

I'm a designer who can code. I work in user experience (design research, design strategy) and user interface (visual design, HTML/CSS) design. I'm also pretty good at JavaScript, PHP (WordPress, Drupal), and content strategy. I'm passionate about theology, faith community, justice, and combining design with those things. I live and work in Minneapolis.

Updated on June 17, 2022

Comments

  • Jonathan Stegall
    Jonathan Stegall almost 2 years

    I'm working on my first Flask app (version 0.10.1), and also my first Python (version 3.5) app. One of its pieces needs to work like this:

    1. Submit a form
    2. Run a Celery task (which makes some third-party API calls)
    3. When the Celery task's API calls complete, send a JSON post to another URL in the app
    4. Get that JSON data and update a database record with it

    Here's the relevant part of the Celery task:

    if not response['errors']: # response comes from the Salesforce API call
        # do something to notify that the task was finished successfully
        message = {'flask_id' : flask_id, 'sf_id' : response['id']}
        message = json.dumps(message)
        print('call endpoint now and update it')
        res = requests.post('http://0.0.0.0:5000/transaction_result/', json=message)
    

    And here's the endpoint it calls:

    @app.route('/transaction_result/', methods=['POST'])
    def transaction_result():
        result = jsonify(request.get_json(force=True))
        print(result.flask_id)
        return result.flask_id
    

    So far I'm just trying to get the data and print the ID, and I'll worry about the database after that.

    The error I get though is this: requests.exceptions.ConnectionError: None: Max retries exceeded with url: /transaction_result/ (Caused by None)

    My reading indicates that my data might not be coming over as JSON, hence the Force=True on the result, but even this doesn't seem to work. I've also tried doing the same request in CocoaRestClient, with a Content-Type header of application/json, and I get the same result.

    Because both of these attempts break, I can't tell if my issue is in the request or in the attempt to parse the response.