Answer a question

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

Answers

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.

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐