Here is the code that should work for sending a file as a base64 string in a form-data request using Postman:
const fs = require('fs');
const path = require('path');
const filePath = 'path_to_your_file'; // Replace with the actual path to your file
const base64Data = fs.readFileSync(filePath, { encoding: 'base64' });
const requestBody = {
mode: 'formdata',
formdata: [
{ key: 'file', value: base64Data },
{ key: 'dc', value: 'someValue' }
]
};
pm.request.body.update(requestBody);
Make sure you replace 'path_to_your_file'
with the actual path to the file you want to upload. This script reads the file, encodes it as a base64 string, and then includes it in the formdata
section of the request body along with the "dc"
key.
Note that directly inserting the base64 string into the "value"
field in your original script might not work as expected, as some APIs might require specific formatting or encoding for file uploads. Using the approach described above ensures that the file is encoded correctly before being included in the form data.