+ Start a Discussion
Sunny NarulaSunny Narula 

Apex Rest Callout GET Method with parameters

Apex Rest Callout GET Method with parameters 

I need to call a GET web service method that contains parameters to be passed along.
When I m calling that from POSTMAN with the access token,etc.. I m getting response
Postman Call

https://apiabcdef.test.abcdexyz.com/api/tcUser?Query=UserCode = 'USER_0706'

But when I m calling this from Apex it is giving a bad request error. code below
 

public static HttpResponse getUserIDFromTCheck(String token, string userCode) {       
    //JSONParser parser = JSON.createParser(token); 
        HttpRequest httpReq = new HttpRequest();                                               
        httpReq.setEndpoint( 'https://apibibforceapi.test.tyrecheck.com/api/tcUser?Query=UserCode = \''+userCode+ '\' ');               
        httpReq.setMethod('GET');
        httpReq.setHeader( 'Content-Type', 'application/json' );    
        httpReq.setHeader( 'Authorization', 'Bearer ' + token );        
        Http http = new Http();         
        HttpResponse httpRes = http.send( httpReq );
     //System.debug('#test2 The status code returned from second service : ' + httpRes.getStatusCode() + ' :  ' + httpRes.getStatus());
return httpRes;  
}

this code gives error as  :
getStatusCode  : 400
getStatus : Bad Request

Please let me know is this the right way to call a GET third party webservice. 

Nithesh NNithesh N
Hi Sunny, 

Just to make sure, Did you add 'https://apibibforceapi.test.tyrecheck.com' as remote site in your salesforce org? 
if not, Please add it as remote site setting. 

To add a remote site setting:
  1. From Setup, enter Remote Site Settings in the Quick Find box, then select Remote Site Settings.
  2. Click New Remote Site.
  3. Enter a descriptive term for the Remote Site Name.
  4. Enter the URL for the remote site.
  5. Optionally, enter a description of the site.
  6. Click Save.
Let me know if it works...

Best,
Nithesh
Sunny NarulaSunny Narula

Thanks Nithesh,

yes that is already added as I m calling many other API from same domain

Nithesh NNithesh N
Oh Then the only reason, I think of is...URL.

Check whether the URL has any blank space and the userCode is already a string, so you may not need use those quotes.. Just try these and let me know if any of them works.
Nithesh NNithesh N
My doubt is on the blankspace after UserCode....Clients like Postman tend to be linient with blankspaces sometimes, But they tend to cause trouble in Apex.
Sunny NarulaSunny Narula

Hi Nithesh,
Thanks again for looking so closely to it, but that I discussed that with the API team before posting a question here to developer forum, they wanted and highly recommend for that space.

Even if I remove the space from Postman it does not work in postman too, below is the snapshot from postman removing that space.

User-added image

 

Ira BanerjeeIra Banerjee
Hi,
Do we have any solution for this issue, I am facing the exact same issue while passing string parameters in a 'GET' request. The exact url is working absolutely fine in POSTMAN but not while invoking from Salesforce. All remote site settings setups are successfully done.

Can anyone please help. Need this on an urgent basis.
Xavier RoigXavier Roig
You can use spaces in URL encoding. Replace all the spaces with %20 code.
Example: https://example.com?$parameter=%20value