Is there any way to get html source code of external url using cUrl or rest api. As in php we can get by using following code.

$ch = curl_init("http://www.example-webpage.com/file.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);

Can we achieve something like this in Apex ?

Any help will be appreciated.


1 Answer 1


In the simplest sense, yes you can. You can use the example verbatim from the HTTP classes documentation to send a GET request and return the HTML source as a string.

public class HttpCalloutSample {

  // Pass in the endpoint to be used using the string url
  public String getCalloutResponseContents(String url) {

    // Instantiate a new http object
    Http h = new Http();

     // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
    HttpRequest req = new HttpRequest();

    // Send the request, and return a response
    HttpResponse res = h.send(req);
    return res.getBody();

There are a couple of additional points you need to consider:

  1. You can't make callouts to arbitrary URLs from Salesforce. The domains need to first be configured as trusted under the Remote Site Settings.
  2. Not all contexts support making callouts. For instance, you can't make a callout directly in a Trigger. If you need to the process to start from a trigger you would first need to transition to an asynchronous context.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .