Page 1 of 1

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

Posted: Sun Dec 17, 2023 11:46 am
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?

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

Posted: Mon Dec 18, 2023 10:22 am
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

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

Posted: Mon Dec 18, 2023 10:30 am
by magnussandstrom
Thanks, but where should I place the 'prompt'?

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

Posted: Mon Dec 18, 2023 10:34 am
by magnussandstrom
I also need the form-data key (file) before the file, not only [Job.Path].

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

Posted: Mon Dec 18, 2023 11:36 am
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.

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

Posted: Mon Dec 18, 2023 12:34 pm
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"}

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

Posted: Mon Dec 18, 2023 4:01 pm
by joaodaffonsojr
magnussandstrom,

What I did.
  • Create a JSON file as the API need
  • Config the HTTP request to use this JSON on FilePath

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

Posted: Tue Dec 19, 2023 3:04 pm
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!