HTTP request (Body as form-data including a file) [SOLVED]

Post Reply
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

HTTP request (Body as form-data including a file) [SOLVED]

Post by magnussandstrom »

I'm using an API where I need to send the body as form-data and one of the form fields contains a jpg-file.

This is what a cURL example looks like in the documentation:

Code: Select all

curl -X POST https://api-url \
     -H 'x-api-key: YOUR_API_KEY' \
     -F file=@image.jpg \
     -F 'prompt=your promt \
I've tested the API in Postman and it works just fine.

Now I would like to recreate this in a Switch flow. Is it possible to use HTTP request app to do this somehow?
Last edited by magnussandstrom on Tue Dec 19, 2023 3:04 pm, edited 1 time in total.
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: HTTP request (Body as form-data including a file)

Post by freddyp »

HTTP request with the following settings:
Request type: POST (not POST a body, because you are also posting a file)
Attached file: [Job.Path], that should be the JPG
Use MIME encoding: Yes
File variable: file
Headers: x-api-key:apikey
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: HTTP request (Body as form-data including a file)

Post by magnussandstrom »

Thanks, but where should I place the 'prompt'?
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: HTTP request (Body as form-data including a file)

Post by magnussandstrom »

I also need the form-data key (file) before the file, not only [Job.Path].
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: HTTP request (Body as form-data including a file)

Post by freddyp »

Right, I left that one out: the prompt goes into Parameters.

Background: posting a file with parameters is like posting a form. A MIME is created with the following structure:

Code: Select all

MIME-Version: 1.0
Content-Type: multipart/related; boundary=i7wMePDuTqA3JHHXpDUEXVORVjtiNIdJVvktuGrDPUtnjZLvr

--i7wMePDuTqA3JHHXpDUEXVORVjtiNIdJVvktuGrDPUtnjZLvr
Content-Type: application/PDF
Content-ID: PDF_00001
Content-Disposition: attachment; filename="test.pdf"; name="file"

...here comes the PDF file or whatever other file ...

--i7wMePDuTqA3JHHXpDUEXVORVjtiNIdJVvktuGrDPUtnjZLvr
Content-Disposition: form-data; name="parameter1"

value 1
------i7wMePDuTqA3JHHXpDUEXVORVjtiNIdJVvktuGrDPUtnjZLvr
Content-Disposition: form-data; name="parameter2"

value 2
--i7wMePDuTqA3JHHXpDUEXVORVjtiNIdJVvktuGrDPUtnjZLvr
The boundary can of course be anything and is used by a web server to identify where a part starts and where it ends.

A web server receiving a MIME knows what the parameters are: it is the stuff that is part of Content-Disposition: form-data.

And it knows where the file is because that is in a part with Content-Disposition: attachment. The "name" of the attachment comes from the "File variable" property. Some servers will ignore this, some will not accept the post if there is no attachment with the correct name. In principle there can be multiple attachments in which case it will be necessary to identify the different parts correctly, otherwise the server would not know which part is the cover PDF and which part is the content PDF by way of example . This is not common and is currently not supported by HTTP request.

The difference when using "Request type - POST a body" is that only parameters are placed into the MIME, no attachment.
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: HTTP request (Body as form-data including a file)

Post by magnussandstrom »

I cannot get it to work with HTTP request. I think the issue is that I cannot input the Key for the file (as I can with Postman):

I get {"error":"The request body is not valid. One of the files in the multipart form is not expected"}
Last edited by magnussandstrom on Tue Mar 26, 2024 3:14 pm, edited 1 time in total.
joaodaffonsojr
Member
Posts: 24
Joined: Wed Sep 08, 2021 4:36 pm

Re: HTTP request (Body as form-data including a file)

Post by joaodaffonsojr »

magnussandstrom,

What I did.
  • Create a JSON file as the API need
  • Config the HTTP request to use this JSON on FilePath
Attachments
APi-1.png
APi-1.png (15.59 KiB) Viewed 53430 times
APi-2.png
APi-2.png (28.86 KiB) Viewed 53430 times
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: HTTP request (Body as form-data including a file)

Post by magnussandstrom »

Me and Freddy solved this one (obviously I cannot read):

Request type: POST

Attached file: [Job.Path]
Use MIME encoding: Yes
File variable: file

Parameters: prompt=my prompt text
Headers: x-api-key:1234abcd

Thanks Freddy!
Post Reply