Newer Version Available
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.
-
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:
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 any other feature, you must 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, click .
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.