Example of POST request with Multipart file ! #1471
Replies: 2 comments
-
It did, thanks a lot man :) I can confirm this method works for a FastAPI application that uses UploadFile. |
Beta Was this translation helpful? Give feedback.
-
|
This thread was extremely helpful to me setting up testing for an s3 compatible file upload using presigned POST. Here's some details with an updated method. First, You no longer need to modify Bruno json to enable external libraries. Follow the instructions on this page to enable developer mode and use npm packages. After that, it's nearly the same, The main difference that all my form fields come from the API. Also, for me, adding const fs = require('fs');
const path = require('path');
const FormData = require('form-data');
const data = new FormData();
const absolutePath = path.resolve(bru.cwd(), 'sample.png');
const buffer = fs.createReadStream(absolutePath);
// Append all presigned fields from the previous request
const uploadFields = bru.getVar('upload_fields') || [];
for (const {key, value} of uploadFields) {
data.append(key, value);
}
data.append('file', buffer)
req.setBody(data)Thanks again for finding this in the first place. Saved me some headache. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone !
Here is an example of a functionning request to send Multipart files with Bruno.
As an example, I used a really basic Spring boot application with a PostMapping route that receives a MultipartFile object and a String "sampleValue".
Here is the content of the controller, just for the sake of clarity:
Now, create a Bruno request of POST type with the required url (in my example, "http://localhost:8080/send-file") and set the header "Content-Type": "multipart/form-data" in the relevant tab.
In the "Body" tab, specify the type of file as Multipart. You can also define the parameters, but they will be set in the "Vars" tab, so this is more for documentation purposes.
In the "Vars" tab, specify the variables:
Note: if you're on Windows, you will have to tweak your absolute path a bit. By default, you will have something like "C:\something\somethingelse\yourfile.txt". You will have to either update this to "C:\something\somethingelse\yourfile.txt" or "C:/something/somethingelse/yourfile.txt".
Now, we will create our script:
We're almost ready ! But by default, Bruno is not allowed to access files on your computer, nor is it allowed to use NPM packages without your consent. We do need file system handling and the "form-data" package though. (Note: don't forget to install "form-data" on your computer for this script to work, with
npm i -g form-datafor instance).EXIT BRUNO as the following will corrupt your collection otherwise (at least, that's what happened to me on my first try).
Go to your "bruno.json" file that defines your collection (as a reminder, you can find the path where you stored it in the settings of you collection, in the "Info" tab). Update it to look like the following:
This allows Bruno to use "fs" and "form-data".
Save the file, and you can open Bruno again. Your request is ready !
I hope this helped you :)
Beta Was this translation helpful? Give feedback.
All reactions