0

I'm new to the .NET web api. But I can't figure out what the best practice should be when returning status codes. I've followed the tutorial on creating a web api that supports crud operations to get a good idea on how it all works.

I have a spec where the response to every request returns a status code along with other data, I can change the spec if need be but I can't work out if it's good to return a status code and also return all the data requested or to just return the data on it's own.

For example if I made a request to GetAllCarManufacturers without being authenticated I'd return a custom statusCode of 1 (Indicating not authenticated), and message "User is not authenticated.". But if I was authenticated I'd like to send back a statusCode of 0 (indicating success) and all the car manufacturers. This seems to go against the way the tutorial is organised as only the car manufacturers are sent back without any additional data. Which leads me to believe passing around a statusCode isn't the correct thing to do.

I've seen in the example crud demo that HttpResponseExceptions are thrown which sets the HttpStatusCode to a certain value (see code below). Should I be using that instead of returning my own status code? But then my concern is it doesn't have enough different status codes that will match my custom scenarios.

// Setting the HTTPStatusCode example.
throw new HttpResponseException(HttpStatusCode.NotFound);
1

1 Answer 1

1

.NET Web API sets up a convention for HTTP calls to a server that supports a REST interface. So, if you follow the convention, you should return HTTP Status Codes as a way of indicating what happened to the request when the server processed it.

HTTP Status Codes are part of the HTTP spec and can be found here.

There are many benefits to using HTTP Status Codes. One is that the HTTP Status Code is a header, so the client doesn't have to look into the content of the response in order to find out what happened.

So, returning a custom status code (of say 0 or 1) is not very useful to HTTP clients if they expect a RESTful experience from your interface.

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

7 Comments

Good answer. Only quibble is that the process of returning HTTP status codes has nothing to do with REST. This is just a requirement of using HTTP properly. Web API has absolutely zero opinions about whether you create a RESTful system or not.
Isn't REST just using HTTP properly? :)
:-) I can't tell if you are trolling me or not, but let's just say no REST!=HTTP.
Ha! :) No, not trolling...After reading Fielding's paper, it seems REST is just building client/server applications using Hypermedia over HTTP. Since this is the case, it seems that using proper HTTP codes is a part of the REST story. Or am I thinking about it incorrectly?
Using HTTP codes is part of the REST over HTTP story yes. But that relation doesn't go the other way. You can do valid HTTP without conforming to the REST constraints. Web API was specifically designed to be opinionated about HTTP but have no cares about whether you happen to follow the REST constraints or not. That's one reason why there is little support for hypermedia baked into Web API. Web API doesn't stop you doing REST, but it doesn't encourage it either.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.