/ApexCallouts

A lightweight Apex library for making HTTP callouts. Works with remote site settings and named credentials.

Primary LanguageApexMIT LicenseMIT

Apex Callouts

A lightweight Apex library for making HTTP callouts
Deploy to Salesforce

Using Callout Constructors

  • Callout(String endpoint) - used when you have a full URL for the endpoint and you are using remote site settings
  • Callout(String namedCredential, String endpointPath) - used with named credentials. To use this, the named credential's endpoint should be just the base URL of the API. The specific endpoint resource to call is then provided via the endpointPath parameter.

Building Your Callout Request

Once you have instantiated an instance of Callout, you can setup additional options for the callout, like adding headers & parameters. Each builder method returns the current instance of Callout, allowing you to chain the builder methods.

  • Callout setClientCertificateName(String clientCertificateName)
  • Callout setCompressed()
  • Callout setCompressed(Boolean compress)
  • Callout setHeader(String key, String value)
  • Callout setHeaders(Map<String, String> headers)
  • Callout setParameter(String key, String value)
  • Callout setParameters(Map<String, String> parameters)
  • Callout setTimeout(Integer timeoutMs)

Making Your Callout Request

Once you have instantiated an instance of Callout and setup the headers & parameters (if needed), you can call any of the HTTP verb methods - each method returns an instance of HttpResponse.

  • HttpResponse del() - 'delete' is a reserved word in Apex, so the method name has been abbreviated
  • HttpResponse get()
  • HttpResponse head()
  • HttpResponse patch() or patch(Object requestBody)
  • HttpResponse post() or post(Object requestBody)
  • HttpResponse put() or put(Object requestBody)
  • HttpResponse trace()

PATCH, POST & PUT methods

Patch, post & put methods accept an Object as a parameter, with 3 main types supported

  • Blob: Callout automatically uses setBodyAsBlob(yourBlob)
  • Dom.Document: Callout automatically uses setBodyDocument(yourDocument)
  • Serializable Object: Any other object types will be serialized and sent as JSON, using setBody(JSON.serialize(yourObject))

For all 3 scenarios, the header 'Content-Type' is automatically set based on the request body if the header has not already been set

Error Handling

When the callout is made, any status code >= 400 automatically throws an instance of Callout.HttpResponseException exception

Example Usage

GET request with headers & parameters, using chained method calls & named credentials

HttpResponse myCalloutResponse = new Callout('myExampleNamedCredential', '/fakeResource')
    .addHeader('myHeader', 'myHeaderValue')
    .addParameter('myFirstParameter', 'someValue')
    .addParameter('mySecondParameter', 'anotherValue')
    .get();

GET request with headers & parameters, using chained method calls & a full URL (remote site settings)

HttpResponse myCalloutResponse = new Callout('https://api.example.com/fakeResource')
    .addHeader('myHeader', 'myHeaderValue')
    .addParameter('myFirstParameter', 'someValue')
    .addParameter('mySecondParameter', 'anotherValue')
    .get();