api_post
Execute POST API requests by providing endpoint URL, JSON body, and optional headers. Integrates with browser automation for data submission and API testing.
Instructions
Perform a POST request to an API endpoint
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | API endpoint URL | |
| data | Yes | Request body data (JSON string) | |
| headers | No | Request headers |
Implementation Reference
- src/executor.ts:543-576 (handler)The actual handler function that executes the api_post tool logic. It sends a POST request using Playwright's APIRequestContext, passing the data and headers from the arguments, and returns the response status and body.
async function handleApiPost(client: APIRequestContext, args: any): Promise<{ toolResult: CallToolResult }> { try { const options = { data: args.data, headers: args.headers || { 'Content-Type': 'application/json' } }; const response = await client.post(args.url, options); const responseData = await getResponseData(response); return { toolResult: { content: [ { type: "text", text: `POST ${args.url} - Status: ${response.status()}`, }, ...responseData ], isError: false, }, }; } catch (error) { return { toolResult: { content: [{ type: "text", text: `POST request failed: ${(error as Error).message}`, }], isError: true, }, }; } } - src/tools.ts:147-163 (schema)The schema registration for api_post. It defines the tool name, description, and input schema requiring 'url' (string) and 'data' (string - JSON body), with optional 'headers' (object).
{ name: "api_post", description: "Perform a POST request to an API endpoint", inputSchema: { type: "object", properties: { url: { type: "string", description: "API endpoint URL" }, data: { type: "string", description: "Request body data (JSON string)" }, headers: { type: "object", description: "Request headers", additionalProperties: { type: "string" } } }, required: ["url", "data"] } }, - src/executor.ts:212-213 (registration)The dispatch case in executeToolCall that routes the 'api_post' tool name to the handleApiPost handler function.
case "api_post": return await handleApiPost(apiClient!, args); - src/tools.ts:14-20 (registration)The API_TOOLS list that declares 'api_post' as one of the available API tools.
export const API_TOOLS = [ "api_get", "api_post", "api_put", "api_patch", "api_delete" ]; - src/executor.ts:144-161 (helper)Helper function getResponseData used by handleApiPost to parse and format the HTTP response body based on content type.
async function getResponseData(response: any): Promise<TextContent[]> { const contentType = response.headers()['content-type'] || ''; let responseText: string; if (contentType.includes('application/json')) { try { const json = await response.json(); responseText = JSON.stringify(json, null, 2); } catch (e) { responseText = await response.text(); } } else { responseText = await response.text(); } return [{ type: "text", text: `Response body:\n${responseText}`, } as TextContent]; }