Custom Response Parameters

Created on 24 November, 2024Docs • 12 views • 3 minutes read

When creating a monitoring check in Pingtr, you can define expected HTTP responses and headers to ensure your application behaves as intended.

This feature allows you to validate both the content and structure of responses from your endpoints, ensuring proper functionality under various conditions.

Why Custom HTTP Response Validation Is Important

Custom response validation is a crucial part of monitoring because it enables you to:

  1. Ensure APIs return the correct status codes and headers.
  2. Validate the response body to match the expected structure and content.
  3. Detect unexpected changes in API behavior or content, which might indicate issues like bugs, unauthorized modifications, or downtime.
  4. Monitor compliance with specific API contracts or service-level agreements (SLAs).

Key Aspects of Custom HTTP Response Validation

When configuring a monitoring check, you can define the following:

1. Expected Status Code

The HTTP status code is the first indication of a request's success or failure. Common codes include:

  1. 200 OK: The request was successful.
  2. 201 Created: A resource was successfully created.
  3. 204 No Content: The request succeeded with no additional content.
  4. 400 Bad Request: The server could not process the request due to a client error.
  5. 401 Unauthorized / 403 Forbidden: Access is restricted.
  6. 404 Not Found: The requested resource doesn't exist.
  7. 500 Internal Server Error: An issue occurred on the server.

Example Use Case: For an API endpoint that fetches user details:

  1. Expected status code: 200

2. Response Body Validation

The response body is the main data returned by an API or web service. You can define specific text, JSON fields, or patterns to validate.

Methods to Validate:

  1. Text Matching: Verify that the response contains a specific string or text.
  2. JSON Key Validation: Ensure the response contains specific keys and values.
  3. Regex Matching: Use regular expressions to validate the format of the response (e.g., email, date).

Example: Validate a JSON response for a user API:

{
"user_id": 123,
"name": "John Doe",
"email": "[email protected]"
}

Expected:

  1. "user_id" key exists.
  2. "email" matches the format [email protected].

3. Expected Headers

HTTP headers provide metadata about the response. You can validate headers to ensure the endpoint returns necessary information.

Common Headers to Validate:

  1. Content-Type: Ensure the response content type matches expectations (e.g., application/json or text/html).
  2. Cache-Control: Check for caching instructions.
  3. Authorization: Confirm the presence of required authorization details in responses.
  4. Custom Headers: Verify any custom headers specific to your API.

Example: Validate headers for a REST API:

Content-Type: application/json
Cache-Control: no-cache
X-Custom-Header: Monitoring-Test

Setting Up Custom HTTP Response Monitoring in Pingtr

  1. Navigate to the Check Creation Page:
  2. Select the check type and endpoint to monitor.
  3. Define Expected Response Details:
  4. Input the expected status code(s).
  5. Add expected headers.
  6. Define body validation rules, such as JSON key presence or specific text.
  7. Configure Failure Criteria:
  8. Specify conditions under which the check fails (e.g., missing headers, incorrect status code, or unexpected body content).
  9. Save and Activate the Check:
  10. Pingtr will regularly monitor the endpoint and validate responses.

Examples of Monitoring Use Cases

Example 1: Validate API Data Consistency

  1. Endpoint: https://api.example.com/products
  2. Expected Status Code: 200
  3. Expected Response Body:
  4. JSON contains "product_id" and "price".
  5. Expected Headers:
Content-Type: application/json
Cache-Control: no-cache

Example 2: Validate Authentication Flow

  1. Endpoint: https://api.example.com/login
  2. Expected Status Code: 200
  3. Expected Response Body:
  4. JSON contains "token" key.
  5. "expires_in" is greater than 0.
  6. Expected Headers:
Content-Type: application/json

Example 3: Check for Redirect Handling

  1. Endpoint: https://example.com/redirect
  2. Expected Status Code: 301
  3. Expected Headers:
Location: https://example.com/new-url

Example 4: Validate Server Errors

  1. Endpoint: https://api.example.com/orders
  2. Expected Status Code: 500
  3. Expected Response Body:
  4. Contains "error" key.
  5. "error_code" matches a specific format.

Tips for Effective Response Monitoring

  1. Use Status Codes Effectively: Validate against the expected range (e.g., success codes like 200-299 or error codes like 400-499).
  2. Validate Critical Fields: Focus on fields that directly affect business logic or user experience.
  3. Monitor Headers Consistently: Headers like Content-Type can indicate changes in server behavior or misconfigurations.
  4. Combine Checks: Pair response validation with uptime and performance checks for a holistic monitoring strategy.

By setting custom HTTP response validations, Pingtr helps you ensure your applications, APIs, and services consistently meet expectations and perform reliably.