How to send request
and get response
from server when working with Rest
API.
3 Answers
Http http1=new Http();
HttpRequest req1=new HttpRequest();
req1.setendpoint('URL');
req1.Setheader('String','String');
req1.setmethod('POST'); //you can also SET method `GET` with Get there is no need of req1.setbody()
req1.setbody(string);
HttpResponse res1;
res1 = http1.send(req1);
String str=res1.getbody();
The details of making HTTP callouts in Apex are found in the developer.force.com wiki pages here:
http://wiki.developerforce.com/page/Apex_Web_Services_and_Callouts
-
1Hi Aisha. Welcome to SFSE. As you appear to be new I'd like to invite you to read a little about etiquette and use of stack exchange. The expectations for interaction are a bit more structured than a discussion forum. So when you get a chance, take a look here: salesforce.stackexchange.com/tour .– pchittumDec 13, 2013 at 12:42
These Is How You Can Send A request or get Response
public class pincode {
public string x {get;set;}
public PageReference click() {
http p = new http();// invoiking Http Class
httprequest req= new httprequest(); //Invoking RestRequest
req.setEndpoint('http://pin-codes.in/api/pincode/500072');//Setting Endpoint
req.setMethod('GET');// Setting Method
httpresponse res = new httpresponse();
res=p.send(req);
system.debug('-------'+req.getBody());
x= res.getbody();// Catching Response
return null;
}
}