+ Start a Discussion
Wai Ming Fong 33Wai Ming Fong 33 

REST callout - Raw Request

Hi,

Is there a way to view the raw REST API request sent from Salesforce?

Background:
I am making a GET request to an external system. I have properly formed the request and successfully connected using the HTTPRequester Firefox addon. The raw request in HTTPRequester looks like this:

GET https://apius.uniprix.com/2_0_1/service/account/?t=6f592bf589bdcb361c8c13224182e75103736e67331a7f4774fe97f577f1d743
Apikey: <apikey>
ip: <ip>
Content-Type: application/json
{"account":{"username":"<username>","token":"6f592bf589bdcb361c8c13224182e75103736e67331a7f4774fe97f577f1d743"}}

I have recreated this request in APEX, but I do not get the same response. My APEX code looks like this:

     HttpRequest req = new HttpRequest();
     req.setMethod('GET');
     req.setEndpoint('https://apius.uniprix.com/2_0_1/service/account/?t=' + <token>);
      
     String API_KEY = <apikey>;
     String API_ADDRESS = <ip>;
     req.setHeader('Apikey', API_KEY);  
     req.setHeader('ip', API_ADDRESS);  
     req.setHeader('Content-Type', 'application/json');  

     System.debug('{ "account":{"username":"'+ <username> +'","token":"'+ <token>+'"}}');
     req.setBody('{ "account":{"username":"'+ <username> +'","token":"'+ <token> +'"}}');
         
     Http http = new Http();
     System.debug(req);
     HTTPResponse res = http.send(req);

The request does provide a valid response (so the token seems valid). Instead of the 200 code that HTTPRequester returns though, I am getting a 417 code with the message: "Missing data : account->serviceId"

This leads me to think that the request is being processed as a "POST" instead of a "GET" so I would like to see the raw REST request being sent out from Salesforce.

Open to any ideas.

Thanks,

Ming
Best Answer chosen by Wai Ming Fong 33
Daniel BallingerDaniel Ballinger
I notice that you are setting the Body of the HTTP request. A GET can't have a body defined.

Are you sure you don't actually want a POST request so that the body you have defined is sent as well?

If you look in the debug logs you will see the CALLOUT_REQUEST event. The message for this will tell you the Endpoint and Method (GET/POST) that were used.

All Answers

Sandeep PatwardhanSandeep Patwardhan
you can try using "Postman" extension on chrome. It should give you all the details of REST request.
Daniel BallingerDaniel Ballinger
I notice that you are setting the Body of the HTTP request. A GET can't have a body defined.

Are you sure you don't actually want a POST request so that the body you have defined is sent as well?

If you look in the debug logs you will see the CALLOUT_REQUEST event. The message for this will tell you the Endpoint and Method (GET/POST) that were used.
This was selected as the best answer
Wai Ming Fong 30Wai Ming Fong 30
Daniel. That was it. Not sure what I was thinking. Thanks for the input.