Skip to content

UTIL_HttpClient.RequestBuilder

Class

apex
global inherited sharing class UTIL_HttpClient.RequestBuilder

Fluent builder for configuring and executing HTTP requests through API_Dispatcher.

Example

apex
Map<String, Object> result = UTIL_HttpClient.get('MyService', '/status')
   .header('X-Custom', 'value')
   .timeout(30000)
   .asMap();

Methods

MethodDescription
global Map<String, Object> asMap()Executes the request and deserializes the response body as an untyped Map.
global String asString()Executes the request and returns the response body as a String.
global UTIL_HttpClient.RequestBuilder body(Object requestBody)Sets the request body by JSON-serializing the provided object.
global UTIL_HttpClient.RequestBuilder body(String requestBody)Sets the request body as a raw string.
global UTIL_HttpClient.RequestBuilder credential(String credential)Sets the Named Credential for the endpoint.
global Object deserialize(Type responseType)Executes the request and deserializes the response body into the specified type.
global UTIL_HttpClient.RequestBuilder header(String name, String value)Adds a single header to the request.
global UTIL_HttpClient.RequestBuilder headers(Map<String, String> headerMap)Adds multiple headers to the request.
global API_Outbound invoke()Executes in delegation mode and returns the delegate handler instance.
global UTIL_HttpClient.RequestBuilder method(API_Base.HttpMethod httpMethod)Sets the HTTP method for the request.
global UTIL_HttpClient.RequestBuilder onFailure(UTIL_HttpClient.FailureAction action)Sets the failure handling strategy.
global UTIL_HttpClient.RequestBuilder path(String urlPath)Sets the URL path for the request.
global UTIL_HttpClient.RequestBuilder pathParam(String name, String value)Replaces a {name} placeholder in the URL path with a value.
global UTIL_HttpClient.RequestBuilder queryParam(String name, String value)Adds a query parameter to the request URL.
global UTIL_HttpClient.RequestBuilder replaceRequestToken(String searchToken, String replaceToken)Registers a single request body replacement token.
global UTIL_HttpClient.RequestBuilder replaceRequestTokens(Map<String, String> tokens)Registers multiple request body replacement tokens.
global UTIL_HttpClient.RequestBuilder replaceResponseToken(String searchToken, String replaceToken)Registers a single response body replacement token.
global UTIL_HttpClient.RequestBuilder replaceResponseTokens(Map<String, String> tokens)Registers multiple response body replacement tokens.
global UTIL_HttpClient.RequestBuilder retryOn(Set<Integer> statusCodes)Restricts retry to specific HTTP status codes.
global HttpResponse send()Executes the HTTP request and returns the raw HttpResponse.
global UTIL_HttpClient.RequestBuilder skipLogging()Skips ApiCall__c persistence for this request.
global UTIL_HttpClient.RequestBuilder timeout(Integer milliseconds)Sets the request timeout in milliseconds.
global UTIL_HttpClient.RequestBuilder withCircuitBreaker()Enables circuit breaker protection using the credential name as identifier.
global UTIL_HttpClient.RequestBuilder withCircuitBreaker(String circuitName)Enables circuit breaker protection with a custom identifier.
global UTIL_HttpClient.RequestBuilder withCorrelationId(String correlationId)Sets a correlation ID for cross-transaction log tracing.
global UTIL_HttpClient.RequestBuilder withExponentialBackoff(Integer maxRetries, Integer baseBackoffSeconds)Enables synchronous retry with exponential backoff.
global UTIL_HttpClient.RequestBuilder withIdempotencyKey(String idempotencyKey)Sets an explicit idempotency key for duplicate detection.
global UTIL_HttpClient.RequestBuilder withMockResponse(API_MockFactory.MockResponse mockResponse)Sets a mock response to inject at framework level, overriding the handler's own mock.
global UTIL_HttpClient.RequestBuilder withParameter(String name, String value)Adds a request parameter for subscriber handler mode.
global UTIL_HttpClient.RequestBuilder withParameters(Map<String, String> parameters)Adds multiple request parameters for subscriber handler mode.
global UTIL_HttpClient.RequestBuilder withRetry(Integer maxRetries)Enables synchronous retry with the specified maximum attempts.
global UTIL_HttpClient.RequestBuilder withRetry(Integer maxRetries, Integer backoffSeconds)Enables synchronous retry with a linear backoff.
global UTIL_HttpClient.RequestBuilder withTriggeringRecord(Id recordId)Links this callout to a triggering business record.

asMap

apex
global Map<String, Object> asMap()

Executes the request and deserializes the response body as an untyped Map.

Returns Object — The deserialized response as Map of String to Object

Example

apex
Map<String, Object> data = UTIL_HttpClient.get('MyService', '/data').asMap();

asString

apex
global String asString()

Executes the request and returns the response body as a String.

Returns String — The response body string

Example

apex
String body = UTIL_HttpClient.get('MyService', '/data').asString();

body

apex
global UTIL_HttpClient.RequestBuilder body(Object requestBody)

Sets the request body by JSON-serializing the provided object.

Parameters

ParameterTypeDescription
requestBodyObjectThe object to serialize as JSON

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .body(new Map<String, String>{'key' => 'value'})
   .send();
apex
global UTIL_HttpClient.RequestBuilder body(String requestBody)

Sets the request body as a raw string.

Parameters

ParameterTypeDescription
requestBodyStringThe raw body string

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .body('{"key": "value"}')
   .send();

credential

apex
global UTIL_HttpClient.RequestBuilder credential(String credential)

Sets the Named Credential for the endpoint.

Parameters

ParameterTypeDescription
credentialStringThe Named Credential name

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.useHandler(API_SendEmail.class)
   .credential('EmailGateway')
   .invoke();

deserialize

apex
global Object deserialize(Type responseType)

Executes the request and deserializes the response body into the specified type.

Parameters

ParameterTypeDescription
responseTypeTypeThe Apex Type to deserialize into

Returns Object — The deserialized response object

Example

apex
MyDTO result = (MyDTO)UTIL_HttpClient.get('MyService', '/data')
   .deserialize(MyDTO.class);
apex
global UTIL_HttpClient.RequestBuilder header(String name, String value)

Adds a single header to the request.

Parameters

ParameterTypeDescription
nameStringThe header name
valueStringThe header value

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .header('X-Custom', 'value')
   .send();

headers

apex
global UTIL_HttpClient.RequestBuilder headers(Map<String, String> headerMap)

Adds multiple headers to the request.

Parameters

ParameterTypeDescription
headerMapMapMap of header names to values

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .headers(new Map<String, String>{'X-Custom' => 'value', 'X-Trace' => 'abc'})
   .send();

invoke

apex
global API_Outbound invoke()

Executes in delegation mode and returns the delegate handler instance.

Returns API_Outbound — The executed API_Outbound delegate handler

Example

apex
API_Outbound handler = UTIL_HttpClient.useHandler(API_SendEmail.class)
   .withTriggeringRecord(recordId)
   .invoke();
API_SendEmail.DTO_Response dto = (API_SendEmail.DTO_Response)handler.responsePayload;

method

apex
global UTIL_HttpClient.RequestBuilder method(API_Base.HttpMethod httpMethod)

Sets the HTTP method for the request.

Parameters

ParameterTypeDescription
httpMethodAPI_Base.HttpMethodThe HTTP method enum value

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/data')
   .method(API_Base.HttpMethod.PUT)
   .send();

onFailure

apex
global UTIL_HttpClient.RequestBuilder onFailure(UTIL_HttpClient.FailureAction action)

Sets the failure handling strategy.

Parameters

ParameterTypeDescription
actionUTIL_HttpClient.FailureActionThe failure action to apply

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .onFailure(UTIL_HttpClient.FailureAction.LOG_FAILURE)
   .send();

path

apex
global UTIL_HttpClient.RequestBuilder path(String urlPath)

Sets the URL path for the request.

Parameters

ParameterTypeDescription
urlPathStringThe URL path to append

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/api')
   .path('/v2/send')
   .send();

pathParam

apex
global UTIL_HttpClient.RequestBuilder pathParam(String name, String value)

Replaces a {name} placeholder in the URL path with a value.

Parameters

ParameterTypeDescription
nameStringThe placeholder name (without braces)
valueStringThe replacement value

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.get('CRM', '/accounts/{id}')
   .pathParam('id', accountId)
   .send();

queryParam

apex
global UTIL_HttpClient.RequestBuilder queryParam(String name, String value)

Adds a query parameter to the request URL.

Parameters

ParameterTypeDescription
nameStringThe parameter name
valueStringThe parameter value

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.get('MyService', '/search')
   .queryParam('q', 'test')
   .queryParam('limit', '10')
   .send();

replaceRequestToken

apex
global UTIL_HttpClient.RequestBuilder replaceRequestToken(String searchToken, String replaceToken)

Registers a single request body replacement token. The search token in the serialized request body will be replaced with the replace token before sending.

Parameters

ParameterTypeDescription
searchTokenStringThe token to search for in the request body
replaceTokenStringThe token to replace it with

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('PaymentGateway', '/charges')
   .body(chargeRequest)
   .replaceRequestToken('currencyIsoCode', 'currency')
   .send();

replaceRequestTokens

apex
global UTIL_HttpClient.RequestBuilder replaceRequestTokens(Map<String, String> tokens)

Registers multiple request body replacement tokens. Each search token in the serialized request body will be replaced with its corresponding replace token before sending.

Parameters

ParameterTypeDescription
tokensMapMap of search tokens to their replacement values

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('PaymentGateway', '/charges')
   .body(chargeRequest)
   .replaceRequestTokens(new Map<String, String>{'currencyIsoCode' => 'currency', 'groupName' => 'group'})
   .send();

replaceResponseToken

apex
global UTIL_HttpClient.RequestBuilder replaceResponseToken(String searchToken, String replaceToken)

Registers a single response body replacement token. The search token in the response body will be replaced with the replace token after receiving.

Parameters

ParameterTypeDescription
searchTokenStringThe token to search for in the response body
replaceTokenStringThe token to replace it with

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
HttpResponse response = UTIL_HttpClient.get('PaymentGateway', '/charges')
   .replaceResponseToken('currency', 'currencyIsoCode')
   .send();

replaceResponseTokens

apex
global UTIL_HttpClient.RequestBuilder replaceResponseTokens(Map<String, String> tokens)

Registers multiple response body replacement tokens. Each search token in the response body will be replaced with its corresponding replace token after receiving.

Parameters

ParameterTypeDescription
tokensMapMap of search tokens to their replacement values

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
HttpResponse response = UTIL_HttpClient.get('PaymentGateway', '/charges')
   .replaceResponseTokens(new Map<String, String>{'currency' => 'currencyIsoCode', 'group' => 'groupName'})
   .send();

retryOn

apex
global UTIL_HttpClient.RequestBuilder retryOn(Set<Integer> statusCodes)

Restricts retry to specific HTTP status codes.

Parameters

ParameterTypeDescription
statusCodesSetThe status codes eligible for retry

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .withRetry(3)
   .retryOn(new Set<Integer>{500, 503})
   .send();

send

apex
global HttpResponse send()

Executes the HTTP request and returns the raw HttpResponse.

Returns HttpResponse — The HttpResponse from the callout

Example

apex
HttpResponse response = UTIL_HttpClient.get('MyService', '/data').send();

skipLogging

apex
global UTIL_HttpClient.RequestBuilder skipLogging()

Skips ApiCall__c persistence for this request.

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.get('MyService', '/healthCheck')
   .skipLogging()
   .send();

timeout

apex
global UTIL_HttpClient.RequestBuilder timeout(Integer milliseconds)

Sets the request timeout in milliseconds.

Parameters

ParameterTypeDescription
millisecondsIntegerThe timeout value

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.get('MyService', '/data')
   .timeout(30000)
   .send();

withCircuitBreaker

apex
global UTIL_HttpClient.RequestBuilder withCircuitBreaker()

Enables circuit breaker protection using the credential name as identifier.

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .withCircuitBreaker()
   .send();
apex
global UTIL_HttpClient.RequestBuilder withCircuitBreaker(String circuitName)

Enables circuit breaker protection with a custom identifier.

Parameters

ParameterTypeDescription
circuitNameStringThe circuit breaker identifier

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .withCircuitBreaker('payment-gateway')
   .send();

withCorrelationId

apex
global UTIL_HttpClient.RequestBuilder withCorrelationId(String correlationId)

Sets a correlation ID for cross-transaction log tracing.

Parameters

ParameterTypeDescription
correlationIdStringThe correlation identifier

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .withCorrelationId('txn-12345')
   .send();

withExponentialBackoff

apex
global UTIL_HttpClient.RequestBuilder withExponentialBackoff(Integer maxRetries, Integer baseBackoffSeconds)

Enables synchronous retry with exponential backoff.

Parameters

ParameterTypeDescription
maxRetriesIntegerThe maximum number of retry attempts
baseBackoffSecondsIntegerThe base backoff period in seconds

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .withExponentialBackoff(3, 2)
   .send();

withIdempotencyKey

apex
global UTIL_HttpClient.RequestBuilder withIdempotencyKey(String idempotencyKey)

Sets an explicit idempotency key for duplicate detection. When set, this key takes priority over auto-generated keys.

Parameters

ParameterTypeDescription
idempotencyKeyStringThe idempotency key value

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('PaymentGateway', '/charges')
   .body(chargeRequest)
   .withIdempotencyKey('order-12345')
   .send();

withMockResponse

apex
global UTIL_HttpClient.RequestBuilder withMockResponse(API_MockFactory.MockResponse mockResponse)

Sets a mock response to inject at framework level, overriding the handler's own mock.

Parameters

ParameterTypeDescription
mockResponseAPI_MockFactory.MockResponseThe mock response to inject

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
API_MockFactory.MockResponse mock = new API_MockFactory.MockResponse();
mock.body = '{"id": "12345"}';
UTIL_HttpClient.post('MyService', '/send')
   .withMockResponse(mock)
   .send();

withParameter

apex
global UTIL_HttpClient.RequestBuilder withParameter(String name, String value)

Adds a request parameter for subscriber handler mode.

Parameters

ParameterTypeDescription
nameStringThe parameter name
valueStringThe parameter value

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.useHandler(API_SendEmail.class)
   .withParameter(API_SendEmail.PARAM_SUBJECT, 'Hello')
   .invoke();

withParameters

apex
global UTIL_HttpClient.RequestBuilder withParameters(Map<String, String> parameters)

Adds multiple request parameters for subscriber handler mode.

Parameters

ParameterTypeDescription
parametersMapMap of parameter names to values

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
Map<String, String> parameters = new Map<String, String>{'key' => 'value'};
UTIL_HttpClient.useHandler(API_SendEmail.class)
   .withParameters(parameters)
   .invoke();

withRetry

apex
global UTIL_HttpClient.RequestBuilder withRetry(Integer maxRetries)

Enables synchronous retry with the specified maximum attempts.

Parameters

ParameterTypeDescription
maxRetriesIntegerThe maximum number of retry attempts

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .withRetry(3)
   .send();
apex
global UTIL_HttpClient.RequestBuilder withRetry(Integer maxRetries, Integer backoffSeconds)

Enables synchronous retry with a linear backoff.

Parameters

ParameterTypeDescription
maxRetriesIntegerThe maximum number of retry attempts
backoffSecondsIntegerThe backoff period in seconds

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .withRetry(3, 5)
   .send();

withTriggeringRecord

apex
global UTIL_HttpClient.RequestBuilder withTriggeringRecord(Id recordId)

Links this callout to a triggering business record.

Parameters

ParameterTypeDescription
recordIdIdThe Salesforce record Id

Returns UTIL_HttpClient.RequestBuilder — This builder for method chaining

Example

apex
UTIL_HttpClient.post('MyService', '/send')
   .withTriggeringRecord(record.Id)
   .send();