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
The phrase 'charset=utf-8' should be within 'Content-Type' header, like this: "Content-Type: application/json; charset=utf-8"
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
.
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'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With