MaartenGr/ML-API

Bad request when using curl

BrambleXu opened this issue · 0 comments

First thanks for your great article in Medium!
I have a problem when implementing your code.

I use your test code as test_api.py:

import requests
to_predict_dict = {'satisfaction_level': 0.38,
                   'last_evaluation': 0.53,
                   'number_project': 2,
                   'average_montly_hours': 157,
                   'time_spend_company': 3,
                   'Work_accident': 0,
                   'promotion_last_5years': 0,
                   'sales': 'support',
                   'salary': 'low'}

url = 'http://127.0.0.1:8000/predict'
r = requests.post(url, json=to_predict_dict)
print(r.json())

It return prediction after excuting.

$ python test_api.py
{'prediction': 1}

And the server log seems work well

INFO:     127.0.0.1:49703 - "POST /predict HTTP/1.1" 200 OK

But when I use the Swagger interface, it returns {"detail":"There was an error parsing the body"}

image

Using the curl is the same error.

$ curl -X POST "http://127.0.0.1:8000/predict" -H "accept: application/json" -H "Content-Type: application/json; charset=UTF-8" -d "{'satisfaction_level': 0.38, 'last_evaluation': 0.53, 'number_project': 2, 'average_montly_hours': 157, 'time_spend_company': 3, 'Work_accident': 0, 'promotion_last_5years': 0, 'sales': 'support', 'salary': 'low'}"
{"detail":"There was an error parsing the body"}% 

Where is the problem and how to solve it?
Thanks!