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;
}
}
public class CurrencyConversionService {— the service class.public Decimal convert(Decimal amountInZar, String targetCurrency) {— matching Lesson 1's sketched signature exactly.HttpRequest request = new HttpRequest();— an empty request.request.setEndpoint(— opens the endpoint call.'https://api.example.com/convert?from=ZAR&to=' + targetCurrency + '&amount=' + amountInZar— matches Lesson 1's contract's query parameters, in order.);— closessetEndpoint.request.setMethod('GET');— matches the contract'sGET.- (blank line)
HttpResponse response = new Http().send(request);— sends it.- (blank line)
// ... handling the response comes in the next lessons ...— a placeholder, not yet implemented.return null;— a temporary stand-in return value.}— closes the method.}— 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
}
public Decimal convert(Decimal amountInZar, String targetCurrency) {— the method signature.HttpRequest request = new HttpRequest();— an empty request.request.setEndpoint('https://api.example.com/convert?from=ZAR&to=' + targetCurrency + '&amount=' + amountInZar);— unchanged from the previous section.request.setMethod('GET');— unchanged.- (blank line)
HttpResponse response = new Http().send(request);— sends it.- (blank line)
if (response.getStatusCode() == 200) {— checks whether the callout actually succeeded.System.debug('Callout succeeded: ' + response.getBody());— confirms the mechanics work before adding real logic.}— closes theif.- (blank line)
return null; // parsing comes in Lesson 4— still a placeholder.}— 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?
Build the Callout Service Quiz
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.