UNPKG

directus-extension-request-url-with-more-settings

Version:

Send HTTP requests with advanced settings: redirect behavior, and optional raw body output

104 lines (85 loc) 3.09 kB
# directus-extension-request-url-with-more-settings A Directus Flow operation to send HTTP requests with advanced settings: custom method, URL, headers, body, redirect behavior, and optional raw body output. - Operation type: operation - Language: TypeScript - Uses global fetch (Node.js 18+) - Output shape compatible with Directus built-in “request” operation: - `{ status, statusText, headers, data?, rawBody? }` ## Features - HTTP method: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS (or any custom) - URL - Headers: list of `{ header, value }` - Body (auto-JSON when object and no Content-Type provided) - `followRedirects`: follow or not (manual) redirects - `rawBody`: optionally returns base64-encoded raw response body as a **Data URL** - Smart response parsing: - JSON → parsed into `data` - text/* → parsed into `data` - binary (e.g. image, pdf) → `data` omitted, only `rawBody` returned - Can be used to forward or upload remote files (e.g. fetch image → send to another API) ## Usage in a Flow - Add operation: **HTTP Request (Advanced)** - Options: - Method: e.g., GET - URL: e.g., https://httpbin.org/get?hello=world - Headers: [] - Body: empty or JSON - Follow redirects: true/false - Return raw body (base64 Data URL): true/false ### Output Example with JSON: ```json { "status": 200, "statusText": "OK", "headers": { "content-type": "application/json" }, "data": { "example": "value" } } ``` Example with HTML when `rawBody = true`: ```json { "status": 200, "statusText": "OK", "headers": { "content-type": "text/html" }, "data": "<html>...</html>", "rawBody": "data:text/html;base64,PGh0bWw+Li4uPC9odG1sPg==" } ``` Example with an image file when `rawBody = true`: ```json { "status": 200, "statusText": "OK", "headers": { "content-type": "image/jpeg" }, "rawBody": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ..." } ``` Note: in this case, `data` is omitted because the response is binary. ## Options Reference - `method`: string (default: GET) - `url`: string (required) - `headers`: `Array<{ header: string; value: string }>` - `body`: any - `followRedirects`: boolean (default: true) - `rawBody`: boolean (default: false) ### Behavior - When body is an object and Content-Type isn’t set, it is sent as JSON (`application/json; charset=utf-8`). - When `followRedirects = false`, fetch uses `redirect: "manual"`. - Response parsing: - If `Content-Type` includes `application/json` → parsed as JSON in `data`. - If `Content-Type` starts with `text/` → parsed as text in `data`. - Otherwise (binary file) → omit `data`, only return `rawBody` as base64 Data URL. ## Example Flow - **Trigger**: Webhook (key: test-http) - **Step 1**: HTTP Request (Advanced) - Method: GET - URL: https://httpbin.org/get?hello=world - Follow redirects: true - Raw body: false - **Optional Step 2**: Log - Message: `{{ steps.step1.status }} - {{ steps.step1.data.url }}` ## License MIT Dan Denolf