Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad request is returning 500 status code instead of 400 #1208

Closed
MrRace opened this issue Jul 7, 2020 · 2 comments · Fixed by #1212
Closed

Bad request is returning 500 status code instead of 400 #1208

MrRace opened this issue Jul 7, 2020 · 2 comments · Fixed by #1212
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@MrRace
Copy link

MrRace commented Jul 7, 2020

  1. When post a bad request that should be a json, how can I define the response for the client? It seems just reply "500 Internal Server Error" now. When the client post a request that is not json format, how can I tell the client with the response?
  2. If I do not set the -H "Content-Type: application/json", but the request data is standard json format, the payload in the server is empty. I just print the payload in the predict :
        print("payload type=", type(payload))
        print("payload=", payload)

the print result are below:

payload type= starlette.datastructures.FormData
payload= FormData([]) # it is empty, why?

why the body get by the server is empty?
Thanks a lot!

@MrRace MrRace added the question Further information is requested label Jul 7, 2020
@RobertLucian RobertLucian self-assigned this Jul 7, 2020
@RobertLucian RobertLucian added the bug Something isn't working label Jul 8, 2020
@RobertLucian
Copy link
Member

RobertLucian commented Jul 8, 2020

@MrRace thank you a lot for reporting this. Indeed, this doesn't appear to be the right behavior for the webserver:

  1. When a post request with a malformed JSON is made, a 400 error code should be returned. Instead, it gets a 500.
  2. When the content-type is not set (i.e."Content-Type: "), your payload parameter should be a bytes object.

Also, when the user doesn't set the content-type for a curl request, curl will automatically set it to multipart/form. This can explain why you are getting a starlette.datastructures.FormData object. I presume you were using the curl utility for this. Check this out:
https://stackoverflow.com/questions/37718308/what-is-default-header-content-type-for-http-post-method-in-curl-form

@RobertLucian RobertLucian changed the title define own response for the bad request Bad request is returning 500 status code instead of 400 Jul 8, 2020
@RobertLucian
Copy link
Member

@MrRace we've created PR #1212 to address this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants