0

I searched almost 2-3 hours for proper response code for below POST API but i did not get proper answer so I kindly request someone to help me here.

We have a POST API which creates resources maximum 10 times. When we call POST API for 11th time, we should get a response with message "User exceeded the limit to create resource" and proper response code. Please suggest what should be the proper response code.

1

3 Answers 3

2

With the level of details provided in your question, I would suggest a status code in the 4xx range, along with a response payload that describes the error in a meaningful way for the client.

You could consider 403 (Fobidden), expressing that the server understands the request, but refuses to authorize it. However there might be other status codes more suitable for your situation, depending on what your are trying to achieve:

  • 402 (Payment Required): If the quota of requests has been exceeded, but more requests could be performed upon a payment, you could consider the 402 status code (even though the documentation says it's reserved for future use, its reason phrase is pretty clear and defines well its purpose).

  • 429 (Too Many Requests): If you are applying restrictions on the number of requests per hour/day, the 429 status code may be suitable for your needs. However this status code is used by a server to indicate that too many requests have been received in a short amount of time, that is, the client is throttling.

If these status codes don't seem to match, simply go for 400, which expresses a bad request.


Status codes indicate the result of the attempt to understand and satisfy the request.

But you have to keep in mind that status codes are sometimes not sufficient to convey enough information about an error to be helpful. That's why you are advised to return a payload that describes the error. The RFC 7807 defines a standard for that.

If you create your own status code for that (what you could do, but doesn't mean you should do), be aware that clients will treat unrecognized status codes as being equivalent to the x00 status code of that class. From the RFC RFC 7231:

For example, if an unrecognized status code of 471 is received by a client, the client can assume that there was something wrong with its request and treat the response as if it had received a 400 (Bad Request) status code. The response message will usually contain a representation that explains the status.

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

7 Comments

403 is usually associated with incorrect ( or missing ) authorization details, so I would not use that
Thanks @cassiomolin for your suggestion but I think 403, 402, and 429 are not suitable for my requirement. My requirement is, user is not allowed to do no more API request beyond maximum limit. If API request is made beyond limit then API should return proper response code.
403(resource is authorised to access), 402(if maximum limit is reached, resource cannot be created in any case), and 429(request is not bound to time) so these status codes are not suitable for my requirement.
@BasavarajLamani I would still stick to 403. If it doesn't seem to match, go for 400, which expresses a bad request.
@cassiomolin Thanks, I am also using now 403 with message "user exceeded the limit to create resource"
|
1

You should try to use a 4xx status. Personally, I would use 403 because the user is forbidden to create the object.

The HTTP 403 Forbidden client error status response code indicates that the server understood the request but refuses to authorize it. This status is similar to 401, but in this case, re-authenticating will make no difference. The access is permanently forbidden and tied to the application logic, such as insufficient rights to a resource. source

And then you can add a message to the request body explaining why the request is not successful. Some more info about 403

Also, I like to check this page if I need an overview of all status codes: Status Codes

1 Comment

Thanks @nickoooname, I am also using now 403 with message "user exceeded the limit to create resource"
0

I would suggest 400, Bad Request.

403 is more for authorization issues which this is not. Provide a clear explanation why you're returning 400 and you're good to go.

Yes, it's a more generic solution, which is exactly what makes it more appropriate.

HTTP status codes have very clear use cases which are understood by everyone. It is not advisable to "reuse" one for something else.

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.