I am trying to set the status code to 429 for Too many request using the code below:

if(Limits.getQueries() < Limits.getLimitQueries()){
              upsert UpdateData;
              else if((Limits.getQueries() >= Limits.getLimitQueries()))
                  RestContext.response.statusCode = 429;
                  UpdateError[] err = new UpdateError[]{ 
                  new UpdateReturnError(UpdateData.Id, 'Too many Requests', 'Error updating MessageID ' + msg.MessageId)
                RestContext.response.responseBody = Blob.valueOf(JSON.serialize(err));
                RestContext.response.addHeader('Content-Type', 'application/json');

I am getting the following error in the raw response:

enter image description here

Can someone help me out with this?

| improve this question | | | | |

429 Too Many Requests is not an appropriate status to return from Apex that is processing a REST request, and so presumably that's why the server prevents you from doing so. The relevent RFC indicates that this response is for rate-limiting, ie, the number of HTTP requests received from a single client in a given amount of time:

The 429 status code indicates that the user has sent too many requests in a given amount of time ("rate limiting").

The response representations SHOULD include details explaining the condition, and MAY include a Retry-After header indicating how long to wait before making a new request.

Each REST request will be a separate Apex transaction, and the # of SOQL queries issued is a per-transaction limit. So exceeding that limit would never be because too many calls to your endpoint had come from a single client in a given period of time.

The documentation for the RestResponse class lists the acceptable values for the statusCode property, and 429 is not included.

This should probably be a 200 response, or perhaps a 500. You're successfully receiving and processing the HTTP request. It's just that the result of that request exceeded a limit, but that limit is at a layer much further down than the HTTP server.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.