Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)</p>

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
like image 600
Cathy Avatar asked Feb 01 '19 04:02

Cathy


3 Answers

The phrase 'charset=utf-8' should be within 'Content-Type' header, like this: "Content-Type: application/json; charset=utf-8"

like image 52
haikku Avatar answered Oct 24 '22 19:10

haikku


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.

like image 4
Dave W. Smith Avatar answered Oct 24 '22 18:10

Dave W. Smith


I encountered it in Pytest, solved it by

import json

def test_login():
   payload = {"ecosystem":'abc'}
   accept_json=[('Content-Type', 'application/json;')]
   response = client.post('/data_extraction'), data=json.dumps(payload), headers=accept_json)

   assert response.data == {'foo': 'bar'}
like image 2
Deepak Sharma Avatar answered Oct 24 '22 17:10

Deepak Sharma