HTTP Classes

These classes expose the HTTP request and response functionality.

  • Http Class. Use this class to initiate an HTTP request and response.
  • HttpRequest Class: Use this class to programmatically create HTTP requests like GET, POST, PATCH, PUT, and DELETE.
  • HttpResponse Class: Use this class to handle the HTTP response returned by HTTP.
The HttpRequest and HttpResponse classes support these elements.
  • HttpRequest
    • HTTP request types, such as GET, POST, PATCH, PUT, DELETE, TRACE, CONNECT, HEAD, and OPTIONS
    • Request headers if needed
    • Read and connection timeouts
    • Redirects if needed
    • Content of the message body
  • HttpResponse
    • The HTTP status code
    • Response headers if needed
    • Content of the response body

This example makes an HTTP GET request to the external server passed to the getCalloutResponseContents method in the url parameter. This example also accesses the body of the returned response.

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();
    req.setEndpoint(url);
    req.setMethod('GET');

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

The previous example runs synchronously, meaning no further processing happens until the external web service returns a response. Alternatively, you can use the @future annotation to make the callout run asynchronously.

This example makes an HTTP POST request to the external server passed to the getPostCalloutResponseContents method in the url parameter. Replace Your_JSON_Content with the JSON content that you want to send in the callout.

public class HttpPostCalloutSample {

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

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

    // Instantiate a new HTTP request
    // Specify request properties such as the endpoint, the POST method, etc. 
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('POST');
    req.setHeader('Content-Type', 'application/json');
    req.setBody('{Your_JSON_Content}');

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

To access an external server from an endpoint or a redirect endpoint, add the remote site to a list of authorized remote sites. Log in to Salesforce and from Setup, in the Quick Find box, enter Remote Site Settings, and then select Remote Site Settings.

Use the XML classes or JSON classes to parse XML or JSON content in the body of a request created by HttpRequest, or a response accessed by HttpResponse.

Considerations

  • The AJAX proxy handles redirects and authentication challenges (401/407 responses) automatically. For more information about the AJAX proxy, see AJAX Toolkit documentation.
  • You can set the endpoint as a named credential URL. A named credential URL contains the scheme callout:, the name of the named credential, and an optional path. For example: callout:My_Named_Credential/some_path. A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. Salesforce manages all authentication for Apex callouts that specify a named credential as the callout endpoint so that your code doesn’t have to. You can also skip remote site settings, which are otherwise required for callouts to external sites, for the site defined in the named credential. See Named Credentials as Callout Endpoints.
  • When you set a request body in the callout, set the method to POST. If you set a request body and the request method is GET, a POST request is performed.
  • Callouts are blocked if you have pending uncommitted transactions from DML operations, queueable jobs (that are queued with System.enqueueJob), Database.executeBatch, or future methods.