top of page

Http callout to an endpoint using Apex

//when the callout endpoint's url and authentication settings are done in the remote settings

HttpRequest req = new HttpRequest(); req.setEndpoint('callout:My_Named_Credential/some_path'); req.setMethod('GET'); Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res.getBody());


if (response.getStatusCode() == 200){

List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());

for(Object result : results){

//code for using the result

}

}


//without remote settings HttpRequest req = new HttpRequest(); req.setEndpoint('https://my_endpoint.example.com/some_path'); req.setMethod('GET'); // Because we didn't set the endpoint as a named credential, // our code has to specify: // - The required username and password to access the endpoint // - The header and header information String username = 'myname'; String password = 'mypwd'; Blob headerValue = Blob.valueOf(username + ':' + password); String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue); req.setHeader('Authorization', authorizationHeader); // Create a new http object to send the request object // A response object is generated as a result of the request Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res.getBody());

Recent Posts

See All
Create a Connected App

A connected app is a framework that enables an external application to integrate with Salesforce using APIs and standard protocols,...

 
 
 
Outbound Messaging

Outbound Messages are SOAP transactions that Salesforce automatically sends to external systems when triggered. use case - when an...

 
 
 

Comments


Post: Blog2_Post

©2020 by SalesforceDemystified. Proudly created with Wix.com

bottom of page