Wednesday, September 18, 2013

HttpRequest Class in Salesforce for generating HttpRequest

Use the HttpRequest class to programmatically create HTTP requests like GET, POST, PUT, and DELETE.

Use the DOM Classes  or Json Classes to Parse XML or JSON content in the body of a request created by HttpRequest.

Methods:

The HttpRequest class contains the following public methods:

NameArgumentsReturn TypeDescription
getBodyStringRetrieves the body of this request.
getBodyAsBlobBlobRetrieves the body of this request as a Blob.
getBodyDocumentDom.DocumentRetrieves the body of this request as a DOM document. Use it as a shortcut for:
String xml = httpRequest.getBody();
Dom.Document domDoc = new Dom.Document(xml);
getCompressedBooleanIf true, the request body is compressed, false otherwise.
getEndpointStringRetrieves the URL for the endpoint of the external server for this request.
getHeaderString keyStringRetrieves the contents of the request header.
getMethodStringReturns the type of method used by HttpRequest. For example:
  • DELETE
  • GET
  • HEAD
  • POST
  • PUT
  • TRACE
setBodyString bodyVoidSets the contents of the body for this request. Limit: 3 MB.
The HTTP request and response sizes count towards the total heap size.
setBodyAsBlobBlob bodyVoidSets the contents of the body for this request using a Blob. Limit: 3 MB.
The HTTP request and response sizes count towards the total heap size.
setBodyDocumentDom.Document documentVoidSets the contents of the body for this request. The contents represent a DOM document. Limit: 3 MB.
setClientCertificateString clientCert
String password
VoidThis method is deprecated. Use setClientCertificateName instead.
If the server requires a client certificate for authentication, set the client certificate PKCS12 key store and password.
setClientCertificateNameString certDevNameVoidIf the external service requires a client certificate for authentication, set the certificate name. See Using Certificates with HTTP Requests.
setCompressedBoolean flagVoidIf true, the data in the body is delivered to the endpoint in the gzip compressed format. If false, no compression format is used.
setEndpointString endpointVoidSets the URL for the endpoint of the external server for this request.
setHeaderString key
String Value
VoidSets the contents of the request header. Limit 100 KB.
setMethodString methodSets the type of method to be used for the HTTP request. For example:
  • DELETE
  • GET
  • HEAD
  • POST
  • PUT
  • TRACE
You can also use this method to set any required options.
setTimeoutInteger timeoutVoidSets the timeout in milliseconds for the request. This can be any value between 1 and 120,000 milliseconds.
toStringStringReturns a string containing the URL for the endpoint of the external server for this request and the method used, for example, Endpoint=http://YourServer, Method=POST

The following example illustrates how you can use an authorization header with a request, and handle the response:

Example:

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

If you need to compress the data you send, use setCompressed, as the following sample illustrates:
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 automatically recognizes the format, uncompresses it, and returns the uncompressed value.

No comments: