Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Microsoft Playwright Execute API Requests

From Leeroopedia
Knowledge Sources
Domains API_Testing, HTTP, REST
Last Updated 2026-02-11 00:00 GMT

Overview

Executing HTTP requests against REST APIs with full control over method, URL, headers, body format, and request options is the core action in any API testing workflow.

Description

At the heart of API testing lies the ability to send HTTP requests to a server and receive responses. A comprehensive HTTP request execution capability must support:

  • All standard HTTP methods: GET, POST, PUT, DELETE, PATCH, and HEAD. Each method has distinct semantics -- GET retrieves data, POST creates resources, PUT replaces resources, PATCH partially updates resources, DELETE removes resources, and HEAD retrieves only headers.
  • URL construction: Combining a base URL with a relative path, and appending query parameters. The framework should handle URL encoding and path joining automatically.
  • Request headers: Setting custom headers for content negotiation, authentication, caching directives, and other HTTP metadata.
  • Request body formats: Supporting JSON serialization, form-encoded data (application/x-www-form-urlencoded), multipart form data (for file uploads), and raw binary or text payloads.
  • Timeout control: Setting per-request timeouts to prevent tests from hanging when services are slow or unresponsive.
  • Redirect handling: Following or limiting HTTP redirects (301, 302, 307, 308) with configurable maximum redirect counts.
  • Error handling: Optionally failing immediately on non-2xx status codes, or returning the response for manual inspection.
  • Retry logic: Configurable automatic retries for transient network failures.

The request execution layer must be composable with the context configuration: headers and authentication set at the context level should be merged with per-request overrides, giving tests fine-grained control when needed while maintaining sensible defaults.

Usage

Apply this principle whenever:

  • You need to test any REST API endpoint.
  • You are verifying CRUD operations on a web service.
  • You need to test different request body formats (JSON, form data, multipart).
  • You are testing redirect behavior, timeout handling, or error scenarios.
  • You need to simulate various HTTP methods beyond simple GET requests.

Theoretical Basis

HTTP request execution in API testing follows the Request-Response model of the HTTP protocol:

// GET request - Retrieve a resource
response = httpClient.get("/api/users/42", {
    headers: { "Accept": "application/json" }
})

// POST request - Create a resource with JSON body
response = httpClient.post("/api/users", {
    data: { name: "Alice", email: "alice@example.com" },
    headers: { "Content-Type": "application/json" }
})

// PUT request - Replace a resource
response = httpClient.put("/api/users/42", {
    data: { name: "Alice Updated", email: "alice-new@example.com" }
})

// DELETE request - Remove a resource
response = httpClient.delete("/api/users/42")

// PATCH request - Partially update a resource
response = httpClient.patch("/api/users/42", {
    data: { email: "newemail@example.com" }
})

// POST with form data
response = httpClient.post("/api/login", {
    form: { username: "alice", password: "secret" }
})

// POST with multipart (file upload)
response = httpClient.post("/api/upload", {
    multipart: {
        file: { name: "photo.png", mimeType: "image/png", buffer: fileBytes }
    }
})

The design follows several important patterns:

  • Method delegation: Convenience methods (get, post, put, delete, patch, head) all delegate to a single underlying fetch/send method, ensuring consistent behavior.
  • URL resolution: Relative URLs are resolved against the context's base URL using standard URL resolution rules.
  • Content negotiation: The body serialization format is determined by which parameter is provided (data for JSON, form for URL-encoded, multipart for file uploads).
  • Redirect transparency: The client follows redirects automatically up to a configurable limit, making redirect chains transparent to the test author.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment