api_post
Send data to an API endpoint using a POST request. Specify the URL, JSON body, and headers to interact with web services and automate API-based tasks effectively.
Instructions
Perform a POST request to an API endpoint
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | Request body data (JSON string) | |
| headers | No | Request headers | |
| url | Yes | API endpoint URL |
Implementation Reference
- src/executor.ts:543-575 (handler)The handler function that executes the api_post tool: sends POST request with provided data and headers, retrieves response, and returns formatted result or error.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 Tool schema definition for api_post, including name, description, and inputSchema with required url and data.{ 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)Switch case in executeToolCall that registers and dispatches the api_post tool call to its handler.case "api_post": return await handleApiPost(apiClient!, args);
- src/tools.ts:14-20 (helper)API_TOOLS constant listing api_post among API tools, used to identify and initialize API client in executor.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 api_post handler to process and format API response body.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]; }