Issue
This question is kind of duplicate but I could not find a solution to it. When I am calling the flask app and passing the JSON data, I am getting the error:
"Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)</p>"
Below is the flask code:
@app.route('/data_extraction', methods=['POST'])
def check_endpoint2():
data= request.json()
result = data['title']
out={"result": str(result)}
return json.dumps(out)
#return 'JSON Posted'
This is how I am calling it from curl
curl -i -H "Content-Type: application/json" charset=utf-8 -X POST -d '{"title":"Read a book"}' 127.0.0.1:5000/data_extraction
I also want to know how can I curl the JSON file(test_data.json), will it be like this?
curl -i -H "Content-Type: application/json" charset=utf-8 -X POST -d @test_data.json 127.0.0.1:5000/data_extraction
Solution
You're mostly there. The problem is the -d
overrides the Content-Type
header that you're providing. Try --data
instead of -d
.
And change data = request.json()
to data = request.json
.
Answered By - Dave W. Smith Answer Checked By - Willingham (WPSolving Volunteer)