• Instructions to Ask a Question

    Click on the "Ask a Question" button and select the application for which you would like to ask questions.

    We have 5 different products namely - Pabbly Connect, Pabbly Subscription Billing, Pabbly Email Marketing, Pabbly Form Builder, Pabbly Email Verification.

    The turnaround time is 24 hrs (Business Hours - 10.00 AM to 6.00 PM IST, Except Saturday and Sunday). So your kind patience will be highly appreciated!

    🚀🚀Exclusive Discount Offer

    Just in case you're looking for any ongoing offers on Pabbly, you can check the one-time offers listed below. You just need to pay once and use the application forever -
     

    🔥 Pabbly Connect One Time Plan for $249 (🏆Lifetime Access) -  View offer 

    🔥 Pabbly Subscription Billing One Time Plan for $249 (🏆Lifetime Access) - View offer

  • Important: Any reported problems and issues with your integration will be reported to you and we will encourage the app developers help to resolve those integration issues.

How to create an action to upload a file (Multipart/form-data)

Status
Not open for further replies.

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:

  1. Validates incoming requests based on a provided API key.
  2. Fetches files from specified URLs, resolving the correct content type and file extension.
  3. Constructs a multipart/form-data payload to upload files to a designated endpoint.
  4. 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:

  1. Request Validation:
    • Validates the API key against a predefined value (CHECK_API_KEY).
  2. Body Parsing:
    • Accepts a JSON body containing fields like file_url, endpoint, and method.
  3. File Retrieval and Processing:
    • Fetches the file from the provided file_url.
    • Determines the content type and file extension using headers or URL parsing.
  4. Multipart Data Construction:
    • Creates a multipart/form-data payload with additional body parameters and the retrieved file.
  5. 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:

  1. Navigate to the Pabbly Connect Developer platform and go to the "Actions" section to set up the action.
  2. Method: Set to POST to trigger the Cloudflare Worker.
  3. URL: The URL of Cloudflare Worker. "https://multipartformdata-fileupload.pabbly.workers.dev/ "
  4. Headers:
    • pabbly_api_key: Provide the API key for validation.
    • Content-Type: Set to application/json

AD_4nXcRXzrvoGFjBjNdIAQHyzjS9TzEAvzp6jsHXvztSUs3-JOkV_hfD7nLEOcguoILRmKUfqQYak3JiNGGstZwza6eFqD4-CWNAWdMBbQgXdkn8fzYzt7vI7pvPVw4f3B8nY0Zuglwl7B32Rcu-oI4WgJNW-bI







JSON Body Example:

AD_4nXeUACO_4MWTUS1N0ybv7G6IhDPjReC7rBrp4FkxwfgJkPuzpalEPG0kfwD49F5XQbegcUxwUBx4qEVG5vOHObwjVcr4XP7EF0_X8KxIHJHGKCMzP_ePd72cjGDNbTAv6Wf3oWpTBI7QV_5SMUzLtui5FBA





JSON Body Parameters:
ParameterTypeDescriptionExample Value
pabbly_api_keyHeaderAn authentication key is needed to validate worker access. It should match the predefined key set in the worker code.
pabbly_api_key: 3be0fa73-bd3b-4953-a5f7-3ebdfae0feea

AuthorizationHeaderCustom header to forward to the target endpoint for further authentication, if required.
Authorization: Bearer 0fe5962c3da5d434c55f

Content-Type

HeaderDefines 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.




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 to be forwarded from the initial request to the destination endpoint. Useful for passing tokens or other sensitive information.

["Authorization"]










Example Workflow​

  1. Receive Request: The Worker listens for incoming POST requests containing the JSON body.
  2. Validate API Key: The pabbly_api_key from the headers is compared with a predefined key.
  3. 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.
  4. Construct Payload: The Worker creates a multipart/form-data payload that includes:
    • Additional body parameters.
    • The retrieved file.
  5. 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:​

  1. Boundary Creation: A unique boundary is generated to separate each part in the form-data.
  2. Additional Body Parameters: Each parameter is added as a separate part in the form-data, formatted as:
AD_4nXdXpvc8VRsaTkkWkVGMX8gVzrE_SuzIH0Yj0YEYFKNeRnx3F_omYXa5Yg52aJrKstD4PfUj3ggthfAvf1w_4qwT0x1_src8oHPOyV8ixoPxw2AAT2yZc0VuOUNmTjGdZ2rqe3eo8g4bjxsmPIWH4CA-h6YI


  1. File Part Addition: The file is appended with its key and a content-disposition that includes the file name:

AD_4nXeixuoDLfRUJ03G3Y46Ef-XJplcCTJsSg4pwe_Fd3hyi96G8jjsXMjy7CgfbpKVmpklZb4e1MKhHUijOdkf-l72cTW_RGWqiqeR9TNukgrZObEcWIfmen1OseWaSO_BW7aMGreIM1Uqo2XupblGOjsIShW7

  1. 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:
Status
Not open for further replies.
Top