1

I'm having an API on which I need to set custom status code. Apart from the normal standard http status codes, is it the best practice to create our own status code?

i.e, If my client is sending a blank parameter (orderID), can I set a random status code number of my own say 123? Or is there any standard way to create customer status codes?

3 Answers 3

1

There are quite a lot of existing codes, so usually you won't need to invent new ones. You might, though, but when you do, make sure to use the right ranges.

For instance, the 200+ range indicates success of any kind, 300+ is a redirect, 400+ range is a client error (bad url format, target not found), and 500+ is a server error.

By following these guidelines, you can use all kinds of clients to communicate to your api. A browser should normally display the results of any 2XX status code, and treat it as a succesful request, even if the particular XX is unknown to it.

For a rather complete list of status codes and their normal meaning, as well as a general description of each of the ranges, see this: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

599 is the highest assigned status code. I wouldn't know if it's allowed to use the 600+ range, but I cannot find anything about it. I cannot imagine that your status wouldn't fit in any of the existing categories, though.

REST: Aside from the return code, you can (should?) also have a look at the different methods. You probably know GET (sending data in url) and POST (sending extra data with the request), because they are commonly used. But you also got PUT and DELETE, which are especially useful for an api like this. If you search for terms like Restfull API, you should get plenty of documentation about this subject. Also, see this W3 link for an overview of request methods.

Bringing it together: For creating a customer through the api, you could send a PUT request with the data of the new customer. The api can return 201 Created with an ID (or slug) for the customer. If you want to delete the customer, send a DELETE request and return 204 No content, since you just need to confirm that the delete succeeded, without sending any content.

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

4 Comments

The list of defined status codes is here: iana.org/assignments/http-status-codes/http-status-codes.xhtml. An no, the 6xx range isn't available; it's invalid.
@JulianReschke Thanks for the link, although I find the complete RFCs a bit harder to read than the nice summary W3 has made in the link I already provided in my answer. Do you have any source saying that 6XX and higher are invalid? I know they are not registered, but are they illegal to use in your own API?
@Golez see 6.1.1 Status Code and Reason Phrase: "The first digit of the Status-Code defines the class of response. [...] There are 5 values for the first digit: [1-5]".
@GOlezTrol what you call the "W3 summary" is simply the old version of the IETF HTTP specification, published on the W3C web site.
1

Or is there any standard way to create customer status codes?

No. I'd go for a generic HTTP error as 400 Bad Request and provide a meaningful error message in the response body. This wasy you don't have to make up HTTP codes as you go, and your consumers can process different error responses identically.

Comments

1

Don't define custom status codes unless you're willing to going through the process of standardizing them (http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p2-semantics-26.html#considerations.for.new.status.codes).

Otherwise just use a generic one and send additional details in the response body.

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.