Creating Apex HTTP Requests

Posted on October 10nd, 2014 at 1:14 pm


Creating an HTTP Request through Apex is extremely simple. In this post, I'll go over a few steps to help you get started. If you've ever worked with something like CURL before, this should be a breeze.

Authorizing a remote site

The first step is authorize your Salesforce org to invoke a remote site. You can do this declaratively, by going to Security Controls > Remote Site Settings on your setup menu and clicking New Remote Site.

Fill out the form with the required information:

Adding a remote site

Create the request

Next we create our request using the HttpRequest class.

HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.google.com');
req.setMethod('GET');

Don't assume you can get away with GET being the default method, you will need to explicitly define your method. Possible values include GET, POST, DELETE, HEAD, PUT and TRACE.

Send the request

Now we will use the Http class to send our request. It will return an HttpResponse object.

HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.google.com');
req.setMethod('GET');

Http http = new Http();
HttpResponse res = http.send(req);

Output the response

To see our result, simply run a debug statement or seomthing similar to output the response body.

HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.google.com');
req.setMethod('GET');

Http http = new Http();
HttpResponse res = http.send(req);
String body = res.getBody();

system.debug('Our response: ' + body);

Result

Here's my result. A quick way to test your request is by using an Apex script. If you're using Maven's Mate, you can quickly execute it with ctrl + shift + x

31.0 APEX_CODE,DEBUG;APEX_PROFILING,DEBUG;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: HttpRequest req = new HttpRequest();
Execute Anonymous: req.setEndpoint('http://www.google.com');
Execute Anonymous: req.setMethod('GET');
Execute Anonymous: 
Execute Anonymous: Http http = new Http();
Execute Anonymous: HttpResponse res = http.send(req);
Execute Anonymous: String body = res.getBody();
Execute Anonymous: 
Execute Anonymous: system.debug('Our response: ' + body);
11:24:44.023 (23477322)|EXECUTION_STARTED
11:24:44.023 (23487516)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
11:24:44.024 (24087375)|CALLOUT_REQUEST|[6]|System.HttpRequest[Endpoint=http://www.google.com, Method=GET]
11:24:44.113 (113405813)|CALLOUT_RESPONSE|[6]|System.HttpResponse[Status=OK, StatusCode=200]
11:24:44.114 (114528625)|USER_DEBUG|[9]|DEBUG|Our response: <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special... (Removed)
11:24:44.156 (116846643)|CUMULATIVE_LIMIT_USAGE
11:24:44.156|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 1 out of 10
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 10
  Number of Mobile Apex push calls: 0 out of 10

11:24:44.156|CUMULATIVE_LIMIT_USAGE_END

11:24:44.116 (116887259)|CODE_UNIT_FINISHED|execute_anonymous_apex
11:24:44.118 (118347905)|EXECUTION_FINISHED

What's next?

In my next post, I'll go over how to parse a JSON response.

comments powered by Disqus