Search code examples
pythonflaskflask-restful

Flask returns no response - Postman 'java.lang.NullPointerException" error


I'm trying to build a flask app that takes a file from the client, process it and send a return response. The problem is the application is giving the right response on a failed case (if no file is received), but, returning no response to client in a success case i.e., after processing the file.

from flask import Flask, request, jsonify

@application.route('/file', methods=['GET', 'POST'])
def sid():
    """
    Process the PDF File, and return response
    :return:
    """
    print(request)
    if request.method == 'POST':
        if request.files:
            result_after_processing = #After processing
            print(result_after_processing) # This is giving expected output
            return jsonify({'result': result_after_processing}, 200)
        else:
            return jsonify({'result': 'No file recieved'}, 400)


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

I used postman to POST the file (though File upload (multipart/for-data)) to the endpoint.

If I POST any object other than a File, like empty or text (a fail case), I'm receiving the {'result': 'No file received'} which is expected. However, when I send the file, java.lang.NullPointerException is the result.

I even tried to hard code the valid case response to {'result': 'HardCodedValue'}, but the Pycharm RESTful service still returns java.lang.NullPointerException. In original, my output, result_after_processing is a JSON.

Note: the java.lang.NullPointerException is on Pro PyCharm's RESTful Webservice, not from Python. Python prints the expected result correctly.

I worked around for 5 hours, trying all possible solutions on the internet but still unsuccessful in figuring out the solution. Please help to give your inputs on this.

Thanks.


Solution

  • Looks like the problem is with Pro PyCharm's "Test RESTful Webservice", I received the expected output when I used POSTMAN.