2

I have a simple api on Salesforce that returns some basic User data when called. It works fine however I noticed that the http status and statusCode are different. Is there something specific I have to do to get the two to be the same?

I'm just passing in a session ID via the Headers and one parameter which is a unique ID.

Here is how I form the response:

// Check if the customer key is null/blank
    if (requestBody.customerKey == null || String.isBlank(requestBody.customerKey)) {
        response.header.success = FALSE;
        response.header.statusCode = 406;
        response.header.error = 'The customerKey is required!';
        return response;
    }

Example request: https://myOrg.com/services/apexrest/retrieveUserDetails?customerKey=

Thanks in advance

Screenshot from Postman

  • 1
    can you post request as well – Oleksandr Berehovskyi Oct 15 '18 at 9:41
  • Hi Oleksandr, yes sure thing. I've updated the question. Thanks – Jay Oct 15 '18 at 9:45
  • Also can you add the apex code ? As I can see its the custom Apex service where you manually set the status code – Pranay Jaiswal Oct 15 '18 at 9:55
  • Sure thing, I have just added the relevant section as the class is a 150 lines+ – Jay Oct 15 '18 at 10:09
1

It works fine however I noticed that the http status and statusCode are different.

You are looking at two different codes here.

  1. 200 is the response code here (can be set by using response.statusCode), which signifies that your request was successful and was processed by the server and thus you are getting the successful response here.
  2. 406, is on the response header (response.header.statusCode, I am not sure though how are you able to set it this way as I am not able to access the header attribute on HttpResponse/RestResponse) and that you are explicitly setting in your post method when you are creating the response

Is there something specific I have to do to get the two to be the same?

It depends on what do you want to pass on response.statusCode vs. response.header.statusCode. Ideally you will want to keep the values same.

  • Jayant, once again thanks so much for the clarification. It's now working as expected. For the record I just had to add: "RestContext.response.statusCode = 406" All the best – Jay Oct 15 '18 at 10:40
0

As I can see from your endpoint : https://myOrg.com/services/apexrest/retrieveUserDetails?customerKey=123456 You are sending customerKey as a URL param and not a request body.

In your Apex code, you are check the request body

requestBody.customerKey == null || String.isBlank(requestBody.customerKey) 

whereas your customerKey exists in your URL.

To retrieve the customerKey from the URL use requestURI

String customerKey= String id = RestContext.request.params.get('customerKey');

If you dont want to change the apex code, then create a JSON/XML based on your apex class

{
"customerKey":12345
}

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.