I have an api that has a catch method:

catch (Exception e) {
    // Error
    System.debug('API - ERROR: ' + e.getMessage()); 
    res.responseBody = Blob.valueOf('ERROR: ' + e.getMessage());
    res.statusCode = 500;

As you can see, I put the error in the response body. However, when I read this response from Postman or another client, the body is empty and the status code is 500... How do you set the response error so that I can actually read what the error is?


If your function's return type is not void, then you'd get back whatever you returned rather than responseBody. You cannot use a non-void return type and responseBody at the same time.

public static void doAction() {
  // ...
  RestContext.response.responseBody = ...

... or ...

public static String doAction() {
  // ...
  return 'Error: '+e.getMessage();
  // ...
  • So RestResponse does absolutely nothing other than set the statusCode?... What is the point of having RestResponse if the only thing that it works for is statusCode? – BlondeSwan Feb 14 at 17:00
  • @BlondeSwan RestResponse works when you have a void return type. There are two methods for returning a value: automatic serialization via a return value, or manual serialization via responseBody. The automatic serialization takes priority over the manual serialization. – sfdcfox Feb 14 at 17:04
  • I have a void return type and am using RestResponse, but it's only good for status code? The RestResponse.response body does absolutely nothing for a client hitting the endpoint? – BlondeSwan Feb 14 at 17:36
  • @BlondeSwan Without seeing more of your code, hard to tell much of anything. Make sure you're using the RestResponse provided by RestContext. If you construct your own, it won't work as you expect. – sfdcfox Feb 14 at 17:45
  • I am using the rest response provided by Rest Context: ``` RestResponse res = RestContext.response; ``` Is my declaration, and if something errors my catch statement I showed above is the last line of that method – BlondeSwan Feb 14 at 17:51

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.