Advanced 35 min read

Build the Callout Service

By the end of this lesson, you'll be able to:

  • Build the HttpRequest matching the contract from the previous lesson
  • Send the request and confirm a successful response is received
  • Keep the callout logic in its own focused service class

Prerequisites: "Design the Integration Contract"

Building the request from the contract

public class CurrencyConversionService {
    public Decimal convert(Decimal amountInZar, String targetCurrency) {
        HttpRequest request = new HttpRequest();
        request.setEndpoint(
            'https://api.example.com/convert?from=ZAR&to=' + targetCurrency + '&amount=' + amountInZar
        );
        request.setMethod('GET');

        HttpResponse response = new Http().send(request);

        // ... handling the response comes in the next lessons ...
        return null;
    }
}
  1. public class CurrencyConversionService { — the service class.
  2. public Decimal convert(Decimal amountInZar, String targetCurrency) { — matching Lesson 1's sketched signature exactly.
  3. HttpRequest request = new HttpRequest(); — an empty request.
  4. request.setEndpoint( — opens the endpoint call.
  5. 'https://api.example.com/convert?from=ZAR&to=' + targetCurrency + '&amount=' + amountInZar — matches Lesson 1's contract's query parameters, in order.
  6. ); — closes setEndpoint.
  7. request.setMethod('GET'); — matches the contract's GET.
  8. (blank line)
  9. HttpResponse response = new Http().send(request); — sends it.
  10. (blank line)
  11. // ... handling the response comes in the next lessons ... — a placeholder, not yet implemented.
  12. return null; — a temporary stand-in return value.
  13. } — closes the method.
  14. } — closes the class.

This directly matches Lesson 1's request contract — GET, the query parameters in the exact order specified, built with Module 36's HttpRequest shape.

Confirming the callout actually succeeds

public Decimal convert(Decimal amountInZar, String targetCurrency) {
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://api.example.com/convert?from=ZAR&to=' + targetCurrency + '&amount=' + amountInZar);
    request.setMethod('GET');

    HttpResponse response = new Http().send(request);

    if (response.getStatusCode() == 200) {
        System.debug('Callout succeeded: ' + response.getBody());
    }

    return null; // parsing comes in Lesson 4
}
  1. public Decimal convert(Decimal amountInZar, String targetCurrency) { — the method signature.
  2. HttpRequest request = new HttpRequest(); — an empty request.
  3. request.setEndpoint('https://api.example.com/convert?from=ZAR&to=' + targetCurrency + '&amount=' + amountInZar); — unchanged from the previous section.
  4. request.setMethod('GET'); — unchanged.
  5. (blank line)
  6. HttpResponse response = new Http().send(request); — sends it.
  7. (blank line)
  8. if (response.getStatusCode() == 200) { — checks whether the callout actually succeeded.
  9. System.debug('Callout succeeded: ' + response.getBody()); — confirms the mechanics work before adding real logic.
  10. } — closes the if.
  11. (blank line)
  12. return null; // parsing comes in Lesson 4 — still a placeholder.
  13. } — closes the method.

At this stage, the method confirms the callout mechanics work — sending a real request and getting a real 200 response back — before adding parsing (Lesson 4) or error handling (Lesson 5) on top of a foundation that's already confirmed to work.

Exercise

As a comment, list the three pieces of the HttpRequest this method builds, matching Lesson 1's contract.

Show hint

Endpoint, method, and what else does the contract specify?

APEX

Build the Callout Service Quiz

1. Why build the callout mechanics first, before adding parsing or error handling?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

This lesson builds the actual HTTP callout matching Lesson 1's contract exactly — Module 36's HttpRequest/HttpResponse pattern, applied to this specific integration.