Cloudflare Worker Upload File (multipart/form-data) Documentation
Overview
This Cloudflare Worker script is designed to process POST requests, retrieve files from URLs, and forward them to a specified endpoint using the multipart/form-data format. The script validates incoming requests with an API key and supports dynamic headers, endpoint configuration, and additional body parameters.Purpose
The script acts as a middleware service, facilitating secure file transfers. It:- Validates incoming requests based on a provided API key.
- Fetches files from specified URLs, resolving the correct content type and file extension.
- Constructs a multipart/form-data payload to upload files to a designated endpoint.
- Forwards headers and data to the target endpoint using the specified HTTP method.
How It Works
The Worker handles incoming HTTP POST requests and processes the following:- Request Validation:
- Validates the API key against a predefined value (CHECK_API_KEY).
- Body Parsing:
- Accepts a JSON body containing fields like file_url, endpoint, and method.
- File Retrieval and Processing:
- Fetches the file from the provided file_url.
- Determines the content type and file extension using headers or URL parsing.
- Multipart Data Construction:
- Creates a multipart/form-data payload with additional body parameters and the retrieved file.
- Forwarding to the Endpoint:
- Uses the provided endpoint and method to send the request, including the specified headers and payload.
Configuration in Pabbly
Configure an HTTP Request in Pabbly:- Navigate to the Pabbly Connect Developer platform and go to the "Actions" section to set up the action.
- Method: Set to POST to trigger the Cloudflare Worker.
- URL: The URL of Cloudflare Worker. "https://multipartformdata-fileupload.pabbly.workers.dev/ "
- Headers:
- pabbly_api_key: Provide the API key for validation.
- Content-Type: Set to application/json
JSON Body Example:
JSON Body Parameters:
Parameter | Type | Description | Example Value | ||||||||
pabbly_api_key | Header | An authentication key is needed to validate worker access. It should match the predefined key set in the worker code. |
| ||||||||
Authorization | Header | Custom header to forward to the target endpoint for further authentication, if required. |
| ||||||||
| Header | Defines the content type of the data being uploaded, such as application/json. |
| ||||||||
endpoint |
|
| | ||||||||
file_url |
|
| | ||||||||
file_key | JSON Field | The key name to use for the file in the form-data. | File_name | ||||||||
body | JSON Field | An optional set of additional parameters to send in the form-data. | { "user_id": "12345" } | ||||||||
method |
|
| "POST" | ||||||||
|
|
|
|
Example Workflow
- Receive Request: The Worker listens for incoming POST requests containing the JSON body.
- Validate API Key: The pabbly_api_key from the headers is compared with a predefined key.
- Fetch File: Uses the provided file_url to download the file. If the content type or file extension is not clearly defined, the Worker attempts to resolve it using the file name and other metadata.
- Construct Payload: The Worker creates a multipart/form-data payload that includes:
- Additional body parameters.
- The retrieved file.
- Forward Request: The constructed payload is forwarded to the specified endpoint with the required headers.
Multipart Form Data Construction
Steps for Multipart Form Data Creation:
- Boundary Creation: A unique boundary is generated to separate each part in the form-data.
- Additional Body Parameters: Each parameter is added as a separate part in the form-data, formatted as:
- File Part Addition: The file is appended with its key and a content-disposition that includes the file name:
- Closing Boundary: The boundary is closed to mark the end of the form data.
Error Handling
The Worker responds with appropriate status codes and messages in case of:- Method not allowed (405): When the request method is not POST.
- Unauthorized (401): When the provided API key is incorrect.
- Invalid JSON (400): When the request body is not valid JSON.
- Missing Required Fields (400): When file_url or endpoint is not provided.
- Failed to Fetch File (500): When the Worker cannot retrieve the file from file_url.
- Header Not Found (400): When a specified header to forward is missing.
Troubleshooting and Best Practices
- Dynamic URL Handling: Ensure that file_url points to a publicly accessible file.
- Header Forwarding: Only forward headers that are required by the endpoint to avoid exposing sensitive information.
- Error Logs: Monitor the Worker’s logs on Cloudflare to troubleshoot issues.
Last edited: