HttpRequest Class
Namespace
Usage
Use the XML classes or JSON classes to parse XML or JSON content in the body of a request created by HttpRequest.
Example
The following example illustrates how you can use an authorization header with a request and handle the response.
public class AuthCallout {
public void basicAuthCallout(){
HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.yahoo.com');
req.setMethod('GET');
// Specify the required user name and password to access the endpoint
// As well as the header and header information
String username = 'myname';
String password = 'mypwd';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
// Create a new http object to send the request object
// A response object is generated as a result of the request
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
}
}Compression
To compress the data you send, use setCompressed.
HttpRequest req = new HttpRequest();
req.setEndPoint('my_endpoint');
req.setCompressed(true);
req.setBody('some post body');
If a response comes back in compressed format, getBody recognizes the format, uncompresses it, and returns the uncompressed value.
HttpRequest Methods
The following are methods for HttpRequest. All are instance methods.
getBodyAsBlob()
Signature
public Blob getBodyAsBlob()
Return Value
Type: Blob
getBodyDocument()
Signature
public Dom.Document getBodyDocument()
Return Value
Type: Dom.Document
Example
Use this method as a shortcut for:
String xml = httpRequest.getBody();
Dom.Document domDoc = new Dom.Document(xml);getCompressed()
Signature
public Boolean getCompressed()
Return Value
Type: Boolean
getEndpoint()
Signature
public String getEndpoint()
Return Value
Type: String
getMethod()
Signature
public String getMethod()
Return Value
Type: String
Usage
Examples of return values:
- DELETE
- GET
- HEAD
- PATCH
- POST
- PUT
- TRACE
setBody(body)
Signature
public Void setBody(String body)
Parameters
- body
- Type: String
Return Value
Type: Void
Usage
Limit: 6 MB for synchronous Apex or 12 MB for asynchronous Apex.
The HTTP request and response sizes count towards the total heap size.
setBodyAsBlob(body)
Signature
public Void setBodyAsBlob(Blob body)
Parameters
- body
- Type: Blob
Return Value
Type: Void
Usage
Limit: 6 MB for synchronous Apex or 12 MB for asynchronous Apex.
The HTTP request and response sizes count towards the total heap size.
setBodyDocument(document)
Signature
public Void setBodyDocument(Dom.Document document)
Parameters
- document
- Type: Dom.Document
Return Value
Type: Void
Usage
Limit: 6 MB for synchronous Apex or 12 MB for asynchronous Apex.
setClientCertificate(clientCert, password)
Signature
public Void setClientCertificate(String clientCert, String password)
Return Value
Type: Void
Usage
If the server requires a client certificate for authentication, set the client certificate PKCS12 key store and password.
setClientCertificateName(certDevName)
Signature
public Void setClientCertificateName(String certDevName)
Parameters
- certDevName
- Type: String
Return Value
Type: Void
Usage
setCompressed(flag)
Signature
public Void setCompressed(Boolean flag)
Parameters
- flag
- Type: Boolean
Return Value
Type: Void
setEndpoint(endpoint)
Signature
public Void setEndpoint(String endpoint)
Parameters
- endpoint
- Type: String
- Possible values for the endpoint:
- Endpoint
URL
https://my_endpoint.example.com/some_path - Named credential URL, which contains the scheme callout, the name of the named credential, and, optionally, an appended
path
callout:My_Named_Credential/some_path
- Endpoint
URL
Return Value
Type: Void
setMethod(method)
Signature
public Void setMethod(String method)
Parameters
- method
- Type: String
- Possible values for the method type include:
- DELETE
- GET
- HEAD
- PATCH
- POST
- PUT
- TRACE
Return Value
Type: Void
Usage
You can also use this method to set any required options.
setTimeout(timeout)
Signature
public Void setTimeout(Integer timeout)
Parameters
- timeout
- Type: Integer
Return Value
Type: Void
toString()
Signature
public String toString()
Return Value
Type: String