# How do developers configure and utilize custom HTTP headers during programmatic Apex REST callouts?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, HTTP Headers, Request Customization

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-developers-configure-and-utilize-custom-http-headers-during-programmatic-apex-rest-callouts

## Answer

**Basics:** HTTP headers carry metadata separate from the body: what format you send (`Content-Type`), what you accept (`Accept`), authentication (`Authorization`), and custom tracking (`X-Correlation-Id`). In Apex callouts you build an `HttpRequest`, set headers, send it with `Http.send`, and read response headers if needed.

**How it works:**
1. Create `HttpRequest`, set endpoint and method.
2. `req.setHeader('Content-Type', 'application/json');` and `req.setHeader('Accept', 'application/json');`
3. Add custom headers: `req.setHeader('X-Correlation-Id', UUID.randomUUID().toString());`
4. For authentication use a Named Credential (`callout:Name/...`) so the `Authorization` header is added by the platform; for API-key APIs put the key in a credential parameter rather than code.
5. Send, then read `res.getHeader('Retry-After')` or `res.getHeaderKeys()`.

**Advanced / deep dive:** Useful headers: `Idempotency-Key` for safe retries, `If-Match`/`ETag` for optimistic concurrency, `Accept-Encoding` with `req.setCompressed(true)` for compressed request bodies, `X-Request-Id` for tracing across systems. In the External Credential model, header values can use credential parameter formulas (for example `{!$Credential.ERP_Auth.ApiKey}`) so secrets are never in Apex. Header names are case-insensitive. Setting the same header twice overwrites it. Some headers are controlled by the platform and cannot be set arbitrarily (verify restrictions in current docs). Set `req.setTimeout(ms)` explicitly (maximum 120,000 ms) so slow services do not eat the cumulative callout time. Avoid logging headers that contain secrets.

**Code / config example:**
```apex
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:ERP_NC/v1/orders');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept', 'application/json');
req.setHeader('X-Correlation-Id', UUID.randomUUID().toString());
req.setHeader('Idempotency-Key', orderExtId);
req.setTimeout(30000);
req.setBody(JSON.serialize(payload));
HttpResponse res = new Http().send(req);
String retryAfter = res.getHeader('Retry-After'); // null if absent
```

**Hidden gotchas & must-remember points:**
- Hardcoding `Authorization` tokens in code is a security failure; use Named Credentials.
- Missing `Content-Type` often causes 415 or silent parse errors.
- `getHeader` returns null if absent; null-check before parsing.
- Default timeout is 10 seconds; maximum is 120 seconds.
- Redirects (3xx) are not followed automatically in every case; handle the `Location` header.
- Unit tests need `HttpCalloutMock`; headers in the mock request can be asserted.

**Best practice:** Set Content-Type and Accept explicitly, always include a correlation ID, and let Named Credentials handle secrets.

---
Source: ForceChallenges · last updated 2026-11-02
