Skip to navigation

httpRequest

The httpRequest action makes arbitrary HTTP calls, allowing you to interact with and validate APIs or other web services directly within your tests.

You can specify a basic GET request using a string shorthand or use an object for more complex requests and validation:

  • String Shorthand: Provide the URL directly as the value for the httpRequest key. This performs a basic GET request to that URL.
  • Object Format: Use an object with the following properties:
    • url: (Required unless using openApi) The target URL. Use an absolute URL, a path that starts with one /, or a value that starts with a variable, such as $API_URL/users. Doc Detective joins a path to the global origin from the configuration and appends its originParams. Without an origin, a path fails the step. With openApi, a path still joins the configuration’s origin, not the OpenAPI server.
    • method: (Optional) The HTTP method (for example, get, post, put, patch, or delete). Case is normalized, so GET also works. Defaults to get.
    • timeout: (Optional) Maximum duration in milliseconds to wait for the request to complete.
    • request: (Optional) An object defining the request details:
      • headers: (Optional) Request headers, as either key-value pairs with string values or a newline-separated string block such as Content-Type: application/json\nAuthorization: Bearer token. In the string form, Doc Detective splits each line on its first colon. Header values can therefore contain colons themselves, including full URLs, timestamps like 12:00:00 GMT, and host:port values.
      • parameters: (Optional) Key-value pairs for query string parameters.
      • body: (Optional) The request body. Can be a string or JSON object.
    • response: (Optional) An object defining expected response validation:
      • headers: (Optional) Key-value pairs for expected response headers. Values must be strings.
      • body: (Optional) Expected response body. Can be a string or JSON object.
      • required: (Optional) Array of field paths that must exist in the response body. Uses dot notation for nested fields (for example, user.name) and bracket notation for array indices (for example, items[0].id). Fields can have any value, including null.
    • allowAdditionalFields: (Optional) Whether the response may contain fields beyond those declared in response.body. Defaults to true. Set it to false to fail the step when the response includes any field not declared in response.body. The check is recursive. It descends into nested objects and names each undeclared field by its dot-path, such as user.extra. It applies only when the top-level response.body declares fields. When response.body is unset or empty, the response has no key constraint and the check passes. A nested empty object still constrains. A response.body of { "user": {} } requires user to be empty, so any field under it fails the step.
    • statusCodes: (Optional) An acceptable HTTP status code, or an array of them. If the response code isn’t in this list, the step fails (default: [200, 201]).
    • openApi: (Optional) Define the request based on an OpenAPI definition. Can be a string (operation ID) or an object:
      • name: (Optional) Name of the registered OpenAPI definition (if multiple are loaded).
      • descriptionPath: (Optional) Path or URL to the OpenAPI description file.
      • operationId: (Required) The ID of the operation to use.
      • server: (Optional) Base server URL for the request. If omitted, Doc Detective uses the first server defined in the OpenAPI description.
      • validateAgainstSchema: (Optional) Which parts of the request and response to validate against the OpenAPI schema (request, response, both, or none). The step fails on a mismatch. Defaults to both.
      • mockResponse: (Optional) When true, skips the HTTP request and uses the response example or schema from the OpenAPI description as the response data. Useful for testing an API that isn’t fully implemented yet. If statusCode isn’t set, Doc Detective uses the first response code defined for the operation.
      • statusCode: (Optional) Response code to use for validation, examples, and status-code checking. Doc Detective adds this code to the step’s accepted statusCodes. Without statusCode or statusCodes, the step accepts the operation’s 2xx response codes instead of [200, 201].
      • useExample: (Optional) Which parts of the request and response to populate with example data from the OpenAPI description (request, response, both, or none). Defaults to none.
      • exampleKey: (Optional) Key of the specific example to use if multiple exist.
      • headers: (Optional) Request headers to add to every request built from the definition, such as an Authorization header, in the same two forms as request.headers. A step-level value overrides a matching header set in your configuration, and request.headers override these by name. Note: Properties like request.headers, request.parameters, request.body can override values from the OpenAPI definition or example.
    • Output Saving: You can also save the response body using path, directory, maxVariation, and overwrite properties. The saved file shows environment variable values as ***NAME***. See Saved files and the httpRequest reference.

Setting Variables: To capture parts of the response for later steps, use the step-level variables object. You can assign values based on the response using expressions like $$response.body, $$response.headers, $$response.status, etc. You can use dot notation for nested JSON fields (for example, $$response.body.user.id).

Depending on which validations you configure, the action also exposes the result of each check in its outputs object. To capture any of them for later steps, use the step-level variables object with the matching expression. Each output is present only when its check applies:

  • $$requiredFieldsPresent: whether every field in response.required was found (when response.required is set).
  • $$bodyMatches: whether the response body matched response.body (when response.body is set).
  • $$headersMatch: whether the expected response.headers were present (when response.headers is set).
  • $$noUnexpectedFields: whether the response contained only expected fields (when allowAdditionalFields is false).
  • $$responseSchemaValid: whether the response matched the OpenAPI schema (when validating the response against an OpenAPI definition).
  • $$variation: the fractional difference between the new response and the reference file (when you save the response with path and a reference file already exists).

For comprehensive options, see the httpRequest reference.

Reports show environment variable values in the URL, headers, body, and response as ***NAME***, including a Basic auth token that holds one. Response checks compare the real values. See Keep secrets out of logs, reports, and captures.

Examples

The docs tests run every example below except the string-or-array body example and the save-and-timeout example.

Basic GET request (string shorthand)

{
"tests": [
{
"steps": [
{
"description": "Perform a basic GET request.",
"httpRequest": "https://reqres.in/api/users?page=2"
}
]
}
]
}

Basic GET request (object format)

{
"tests": [
{
"steps": [
{
"description": "Perform a simple GET request using object format.",
"httpRequest": {
"url": "https://reqres.in/api/users?page=2"
}
}
]
}
]
}

POST request with JSON body

{
"tests": [
{
"steps": [
{
"description": "Create a user via POST request.",
"httpRequest": {
"url": "https://reqres.in/api/users",
"method": "post",
"request": {
"body": {
"name": "morpheus",
"job": "leader"
}
}
}
}
]
}
]
}

PUT request with headers and query parameters

{
"tests": [
{
"steps": [
{
"description": "Update a user via PUT request.",
"httpRequest": {
"url": "https://reqres.in/api/users/2",
"method": "put",
"request": {
"headers": {
"Content-Type": "application/json"
},
"parameters": {
"source": "test"
},
"body": {
"name": "morpheus",
"job": "zion resident"
}
}
}
}
]
}
]
}

Update fields with a PATCH request

A PATCH request sends only the fields you want to change.

{
"tests": [
{
"steps": [
{
"description": "Update a user's job via PATCH request.",
"httpRequest": {
"url": "https://reqres.in/api/users/2",
"method": "patch",
"request": {
"body": {
"job": "captain"
}
}
}
}
]
}
]
}

Delete a resource with a DELETE request

A DELETE request removes a resource. APIs vary in the status they return. Set statusCodes to the codes yours uses, such as 200 or 204.

{
"tests": [
{
"steps": [
{
"description": "Delete a user via DELETE request.",
"httpRequest": {
"url": "https://reqres.in/api/users/2",
"method": "delete",
"statusCodes": [204]
}
}
]
}
]
}

Send request headers as a string block

You can also provide request.headers as a single newline-separated string instead of an object. Doc Detective splits each line on its first colon into a name and value. Values that contain colons, such as callback URLs or timestamps, stay intact.

{
"tests": [
{
"steps": [
{
"description": "Send request headers as a newline-separated string.",
"httpRequest": {
"url": "https://reqres.in/api/users",
"method": "post",
"request": {
"headers": "Content-Type: application/json\nX-Callback: https://example.com/cb",
"body": {
"name": "morpheus",
"job": "leader"
}
}
}
}
]
}
]
}

Send a string or array request body

request.body accepts a JSON object, a JSON array, or a raw string. Doc Detective sends an object or array as JSON. It sends a string unchanged, so set the matching Content-Type header yourself. The same three shapes apply to response.body when you validate the response.

{
"tests": [
{
"steps": [
{
"description": "Send an array body as JSON.",
"httpRequest": {
"url": "https://api.example.com/batch",
"method": "post",
"request": {
"body": [
{ "id": 1, "name": "Neo" },
{ "id": 2, "name": "Trinity" }
]
}
}
}
]
}
]
}
{
"tests": [
{
"steps": [
{
"description": "Send a raw string body with a matching Content-Type.",
"httpRequest": {
"url": "https://api.example.com/graphql",
"method": "post",
"request": {
"headers": { "Content-Type": "application/graphql" },
"body": "{ user(id: 2) { name } }"
}
}
}
]
}
]
}

Validate response status code and body

{
"tests": [
{
"steps": [
{
"description": "Create user and validate response.",
"httpRequest": {
"url": "https://reqres.in/api/users",
"method": "post",
"request": {
"body": {
"name": "morpheus",
"job": "leader"
}
},
"response": {
"body": {
"name": "morpheus",
"job": "leader"
}
},
"statusCodes": [201] // Expect HTTP 201 Created
}
}
]
}
]
}

Use OpenAPI definition (by operation ID)

Assumes an OpenAPI definition with operation ID getUserById is loaded.

{
"tests": [
{
"steps": [
{
"description": "Get user by ID using OpenAPI definition.",
"httpRequest": {
"openApi": "getUserById",
"request": {
"parameters": { // Provide required path parameter
"id": 2
}
}
}
}
]
}
]
}

Use OpenAPI definition (detailed object)

{
"tests": [
{
"steps": [
{
"description": "Get user by ID using specific OpenAPI file.",
"httpRequest": {
"openApi": {
"descriptionPath": "./reqres.openapi.json",
"operationId": "getUserById"
},
"request": {
"parameters": {
"id": 3
}
}
}
}
]
}
]
}

Set a variable from response body

{
"tests": [
{
"steps": [
{
"description": "Create user and capture the ID.",
"httpRequest": {
"url": "https://reqres.in/api/users",
"method": "post",
"request": {
"body": {"name": "neo", "job": "the one"}
},
"statusCodes": [201]
},
"variables": {
"USER_ID": "$$response.body.id"
}
},
{
"description": "Use the captured ID in the next request.",
"httpRequest": {
"url": "https://reqres.in/api/users/$USER_ID", // Use variable in URL
"method": "get"
}
}
]
}
]
}

Save the response body and set a timeout

Save the response with path and directory, and cap the request with a timeout in milliseconds. On later runs, maxVariation and overwrite compare the new response against the saved copy.

{
"tests": [
{
"steps": [
{
"description": "Get user data, save the response, and cap the request time.",
"httpRequest": {
"url": "https://reqres.in/api/users/2",
"method": "get",
"timeout": 5000,
"path": "user_2_response.json",
"directory": "./output/api_responses",
"maxVariation": 0.05,
"overwrite": "aboveVariation"
}
}
]
}
]
}

Validate fields with dynamic values

When testing APIs that return unpredictable values (like timestamps, UUIDs, or session tokens), you can verify that fields exist without asserting their exact values.

{
"tests": [
{
"steps": [
{
"description": "Verify user response includes required fields.",
"httpRequest": {
"url": "https://api.example.com/users/123",
"method": "get",
"response": {
"required": ["id", "email", "createdAt", "sessionToken"]
}
}
}
]
}
]
}

Validate nested fields and arrays

Use dot notation for nested object fields and bracket notation for array elements to validate complex response structures.

{
"tests": [
{
"steps": [
{
"description": "Verify nested user profile fields exist.",
"httpRequest": {
"url": "https://api.example.com/users/123",
"method": "get",
"response": {
"required": [
"user.profile.name",
"user.profile.avatar",
"user.settings.notifications"
]
}
}
},
{
"description": "Verify array elements contain required fields.",
"httpRequest": {
"url": "https://api.example.com/orders",
"method": "get",
"response": {
"required": [
"orders[0].id",
"orders[0].total",
"orders[0].items[0].productId"
]
}
}
}
]
}
]
}

Combine required fields with body validation

You can use both response.required and response.body together to validate that certain fields exist while asserting specific values for others.

{
"tests": [
{
"steps": [
{
"description": "Validate both specific values and required fields.",
"httpRequest": {
"url": "https://api.example.com/users",
"method": "post",
"request": {
"body": {
"username": "testuser",
"role": "admin"
}
},
"response": {
"required": ["sessionToken", "expiresAt", "user.id"],
"body": {
"status": "success",
"user": {
"role": "admin"
}
}
},
"statusCodes": [201]
}
}
]
}
]
}

Note: Fields with null, "", 0, or false values are considered to exist and will pass validation.

Reject unexpected response fields

Set allowAdditionalFields to false to assert that the response contains only the fields you declared in response.body. The step fails if the response includes any extra field.

The check descends into nested objects. It catches an extra field at any depth and names it by its dot-path. A nested empty object is still a constraint. Declaring "user": {} in response.body requires the response’s user object to be empty, so any field it carries fails the step. Only an unset or empty top-level response.body lifts the constraint entirely.

{
"tests": [
{
"steps": [
{
"description": "Fail if the response returns fields beyond those declared.",
"httpRequest": {
"url": "https://api.example.com/users/123",
"method": "get",
"response": {
"body": {
"id": 123,
"name": "morpheus"
}
},
"allowAdditionalFields": false
}
}
]
}
]
}