function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SS KarthickSS Karthick 

Real time example for REST apex callout

Hi folks,
        Can anyone give me the real time example for apex REST callout

I wanna like 
enpoint url and apex code for real time apex callout


Thanks in advance
Karthick
Best Answer chosen by SS Karthick
PmathurPmathur
Here is the simple code sample to understand how REST Callout can be done using apex:

String endPointUrl = 'https://www.google.com/search?q=site:linkedin.com+linkedin+Employee+Size';  
Http h = new Http();
HTTPRequest req = new HTTPRequest();
HTTPResponse resp = new HTTPResponse();
req.setHeader('Content-Type', 'application/xml');
req.setEndpoint(endPointUrl);
req.setMethod('GET');
resp = h.send(req);
String response='';
response = resp.getBody();
system.debug('Response='+response);

Dont forgot to add "https://www.google.com" in your remote site setting.
Let me know if it is heplful.

All Answers

PmathurPmathur
Here is the simple code sample to understand how REST Callout can be done using apex:

String endPointUrl = 'https://www.google.com/search?q=site:linkedin.com+linkedin+Employee+Size';  
Http h = new Http();
HTTPRequest req = new HTTPRequest();
HTTPResponse resp = new HTTPResponse();
req.setHeader('Content-Type', 'application/xml');
req.setEndpoint(endPointUrl);
req.setMethod('GET');
resp = h.send(req);
String response='';
response = resp.getBody();
system.debug('Response='+response);

Dont forgot to add "https://www.google.com" in your remote site setting.
Let me know if it is heplful.
This was selected as the best answer
SS KarthickSS Karthick
@Pmathur,
      can you give me the same for post method ?


Thanks in advance
Karthick
Abhishek_PareekAbhishek_Pareek
Here is sample code for POST method:

HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();

req.setEndpoint('http://yourendpoint.com/newPost');
req.setMethod('POST');
req.setBody('name='+EncodingUtil.urlEncode(name, 'UTF-8')+'&country='+EncodingUtil.urlEncode(country, 'UTF-8'));
req.setCompressed(true);

try {
	res = http.send(req);
} catch(System.CalloutException e) {
	System.debug('Callout error: '+ e);
}
Hit the Like link, if it was helpful.
Lukesh KarmoreLukesh Karmore
hey ,  what is endPointUrl actually is , it is the link of the page  or anything ,
can u clear please  with simple example