@prismatic-io/spectral/dist/clients/http:
createClient
Creates a reusable Axios HTTP client instance configured with a base URL, default headers, timeout, and optional retry logic.
The API’s base URL (e.g.
https://api.acme.com/v2/). All relative request URLs are resolved against this.The type of response to expect. Set to
'json' to automatically parse a JSON response, or 'arraybuffer' for binary data. Passed directly to Axios.Headers to send with every request (e.g. an
Authorization header).URL search parameters appended to every request.
The maximum time in milliseconds to wait for a response. Defaults to no timeout.
When
true, logs every request and response to the console. Useful during development.Configuration that controls if and how failed requests are retried. See RetryConfig below.
Basic usage
HttpClient type
HttpClient is a type alias for an Axios instance:
.get(), .post(), .put(), .patch(), .delete(), .request(), and so on.
RetryConfig
Pass a retryConfig object to createClient to automatically retry failed requests.
exponentialDelay and isNetworkOrIdempotentRequestError
These are re-exported from axios-retry for convenience.
exponentialDelay— A pre-built delay function that doubles the wait after each retry attempt.isNetworkOrIdempotentRequestError— The default retry condition. Returnstruefor network errors and idempotent HTTP methods with 5xx responses.
Authorization helpers
toAuthorizationHeaders
Inspects a Spectral Connection object and returns an Authorization header automatically. The detection order is:
- OAuth access token — if
connection.token.access_tokenis set, returns{ Authorization: "Bearer <token>" }. - API key — if
connection.fields.apiKeyis set, returns{ Authorization: "Bearer <apiKey>" }. - Username / password — if both
connection.fields.usernameandconnection.fields.passwordare set, returns{ Authorization: "Basic <base64(user:pass)>" }. - Error — throws if none of the above are present.
- Bearer token
- API key
- Basic auth
Form data and file uploads
The HTTP client supports multipart form uploads through thesendRawRequest function and the built-in inputs. For direct use, construct a FormData instance and pass it as the request body. The client automatically sets the correct Content-Type: multipart/form-data boundary headers.
The built-in
sendRawRequest function calls an internal toFormData helper that converts a list of {key, value} pairs into a FormData object and uses util.types.toBufferDataPayload to handle file data.handleErrors
A global error handler that enriches Axios errors with structured response details.
message— the error message stringdata— the response bodystatus— the HTTP status codeheaders— the response headers
sendRawRequest
Executes a fully configured HTTP request from a set of input values. Used internally by buildRawRequestAction to power raw request actions.
buildRawRequestAction
Generates a ready-to-use Spectral action that exposes all HTTP request inputs (method, URL, headers, query params, body, form data, file data, timeouts, retries, and response type) and performs the request against a fixed base URL.
Built-in inputs
Theinputs export from the HTTP client package is a map of pre-configured Spectral inputs. You can spread them directly into an action’s inputs object.
url — required string
url — required string
The URL path to call (e.g.
/sobjects/Account). Combined with the client’s baseUrl.method — required string
method — required string
The HTTP method. One of
DELETE, GET, HEAD, LINK, OPTIONS, PATCH, POST, PURGE, PUT, UNLINK.data — optional string
data — optional string
The HTTP body payload to send (e.g.
{"exampleKey": "Example Data"}).formData — optional key-value list
formData — optional key-value list
A key-value list of fields to send as a multipart form upload.
fileData — optional key-value list
fileData — optional key-value list
A key-value list of file contents to send as a multipart form upload. Keys are used as field names.
fileDataFileNames — optional key-value list
fileDataFileNames — optional key-value list
Overrides the file name for each
fileData key. Keys must match fileData keys.headers — optional key-value list
headers — optional key-value list
Additional headers to send with the request (e.g.
User-Agent: curl/7.64.1).queryParams — optional key-value list
queryParams — optional key-value list
Query parameters appended to the URL (e.g.
?key1=value1&key2=value2).responseType — default 'json'
responseType — default 'json'
The expected response format:
arraybuffer, document, json, or text.timeout — optional number (ms)
timeout — optional number (ms)
The maximum time in milliseconds the client will wait for a response.
maxRetries — default 0
maxRetries — default 0
The maximum number of retry attempts. Set to
0 to disable retries.retryDelayMS — default 0
retryDelayMS — default 0
Milliseconds to wait between retries. Only used when
useExponentialBackoff is false.retryAllErrors — default false
retryAllErrors — default false
When
true, retries on all error responses including HTTP 429 and other 4xx codes. When false, only retries on 5xx and network errors.useExponentialBackoff — default false
useExponentialBackoff — default false
When
true, enables exponential backoff between retry attempts. retryDelayMS is ignored when this is enabled.debugRequest — optional boolean
debugRequest — optional boolean
When
true, logs the current request and response to the console.