0

I have created myself a standard asp.net web api project and it has a Post on there.

The post basically will insert some values into a database but before doing so I need to do a number of different checks on an alternative database, if anything doesn't check out I want to return the client with a NON 200 http status code but which should I use?

What is the recommended status code to return? I would also like to return some text with it which would contain additional information that could identify what the error was.

Or is it recommended to return 200 and an additional JSON payload identifying if there was a problem and what it is

Does anyone have any examples??

Thanks in advance

2 Answers 2

4

400 BadRequest could be used if what you are performing is validation logic and this validation logic fails for some reason. As far as the additional information is concerned, you might return some info in the body, for example as JSON payload:

{
    "errorMessage":"Validation failed because user doesn't exist in alternative database"
}

If on the other hand you are attempting to update an entity that doesn't exist, you could also send a 404 status code.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, so its recommended to return 400 rather than 200 and just the payload of errormessage or even errorid for example... In my case I would want to return "Error can't add user because group doesn't exist", when someone tries to add a user to a group that doesn't exist, is this the correct way to go?
You can also just return a simple text string in the ReasonPhraseinstead of having to create a response body.
1

In addition to what Darin stated on this thread, here is a good reference on status codes. I have also found this article valuable in handling errors with ASP.NET WebAPI.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.