Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return HTTP 429?

I'm implementing an API using WCF and the specification says to return HTTP 429 in certain circumstances.

Normally I'd simply write:

throw new WebFaultException(HttpStatusCode.NotFound); 

However the HttpStatusCode enum does not contain a 429.

I can obviously cast to the enum

throw new WebFaultException((HttpStatusCode)429); 

However I'm worried that this will not produce the correct result to the application calling my API.

What's the best way to create extend the HttpStatusCode and send valid (but unsupported) HTTP statuses?

like image 505
Liath Avatar asked Mar 25 '14 13:03

Liath


People also ask

How do I return my 429 status code?

The simplest way to fix an HTTP 429 error is to wait to send another request. Often, this status code is sent with a “Retry-after” header that specifies a period of time to wait before sending another request.

What does the HTTP response 429 indicate?

The HTTP 429 Too Many Requests response status code indicates the user has sent too many requests in a given amount of time ("rate limiting"). A Retry-After header might be included to this response indicating how long to wait before making a new request.


1 Answers

From the C# Language Specification 5.0:

The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type and is a distinct valid value of that enum type.

So this is completely alright to do and would be your best bet:

throw new WebFaultException((System.Net.HttpStatusCode)429); 
like image 130
Derek W Avatar answered Sep 29 '22 05:09

Derek W