Newer Version Available

This content describes an older version of this product. View Latest

HTTP Classes

These classes expose the general HTTP request/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, PUT, and DELETE.
  • HttpResponse Class: Use this class to handle the HTTP response returned by HTTP.
The HttpRequest and HttpResponse classes support the following elements.
  • HttpRequest
    • HTTP request types, such as GET, POST, 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

The following example shows an HTTP GET request made to the external server specified by the value of url that gets passed into the getContent method. This example also shows accessing the body of the returned response.

1public class HttpCalloutSample {
2
3  // Pass in the endpoint to be used using the string url
4  public String getCalloutResponseContents(String url) {
5
6    // Instantiate a new http object
7    Http h = new Http();
8
9     // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
10    HttpRequest req = new HttpRequest();
11    req.setEndpoint(url);
12    req.setMethod('GET');
13
14    // Send the request, and return a response
15    HttpResponse res = h.send(req);
16    return res.getBody();
17  }
18}

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.

Before you can access external servers from an endpoint or redirect endpoint using Apex or another feature, add the remote site to a list of authorized remote sites in the Salesforce user interface. To do this, log in to Salesforce and from Setup, enter Remote Site Settings in the Quick Find box, then select Remote Site Settings.

  • 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. See Named Credentials as Callout Endpoints.

Note

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.