API to Upload File (multipart/form-data) Documentation
Overview
This API is designed to process POST requests, retrieve files from URLs, and upload them to a specified endpoint using the multipart/form-data format. The API validates incoming requests with an API key and supports dynamic headers, endpoint configuration, and additional body parameters.Purpose
This API acts as a middleware service, facilitating secure file transfers to any API that supports multipart/form-data based file uploads. 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 API 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 the method as POST.
- URL: Enter the URL "https://multipartformdata-fileupload.pabbly.workers.dev/ " in the "API Endpoint URL" section.
- Headers:
- pabbly_api_key: Provide the API key for validation.
- Content-Type: Set to application/json
Code:
curl --location 'https://multipartformdata-fileupload.pabbly.workers.dev/' \
--header 'pabbly_api_key: test' \
--header 'Content-Type: application/json' \
--header 'Authorization: <TargetApplicationToken>' \
--data '{
"endpoint": ""https://api.targetapp.com/upload",
"file_url": "https://drive.google.com/file/d/1234/view?usp=sharing,
"headers_to_forward": "authorization",
"method": "POST",
"file_key": "profile_photo",
"body":{
"file_key": "file"
}
}'
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. | pabbly_api_key: {{PabblyPrivateKeyForInternalUse}} NOTE: This key is only used for our internal development for now. |
Authorization | Header | Custom header to forward to the target endpoint for further authentication, if required. | Authorization: {{BearerTokenofApplication}} |
Content-Type | Header | Defines the content type of the data being uploaded, such as application/json. | Content-Type: application/json |
endpoint | JSON Field | Specifies the destination endpoint where the file should be uploaded. Accepts the file path with placeholders for the file name. | "https://target-endpoint.com/api/files/upload/:filename.jpeg" |
file_url | JSON Field | URL of the file to be fetched and uploaded to the destination endpoint. The file is downloaded by the worker before being forwarded. | |
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 | JSON Field | Specifies the HTTP method for the request to the destination endpoint. Accepted values are POST, PUT, etc. | "POST" |
headers_to_forward | JSON Field | An array of headers is to be forwarded from the initial request to the destination endpoint. Useful for passing tokens or other sensitive information. | ["Authorization"] |
file_url parameter conditions:
- The file_url can be a direct URL. E.g.
- The file_url can be a direct downloadable drive URL. E.g. https://drive.google.com/uc?export=download&id=1Pn5_9X1JK08t7mw5LR6CnyNes_aAMViy or
https://docs.google.com/spreadsheet...TCfbyoz92Jcl0xyt0cRecM3Ecw/export?format=xlsx
- The file_url can be a direct downloadable Dropbox URL. In the case of a normal Dropbox URL, set the value of the ‘dl’ parameter to 1. E.g. https://www.dropbox.com/scl/fi/orsk...ey=4z45zix70yzh5p4sv9cj4f2os&st=2os6wma0&dl=1
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.
Last edited by a moderator: