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;
    }
}

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
}

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.