Pre-request script: send multipart/form-data

I am trying to fetch a file from box & then upload it another server. This complete process is being done in pre-request script. I was able to successfully get the file buffer from box but I am struck at uploading the file to another server,as I need to send the data as formdata which needs multipart boundary.

If I use postman to upload file, then boundary is automatically calculated by postam. How to calculate/prepare multipart form-data in pre-request script?

@vikasreddyCapb Welcome to the community :partying_face:

You can something like below:

const reqObj = {
    'method': 'POST',
    'url': 'https://postman-echo.com/post',
    'body': {
            'mode': 'formdata',
            'formdata': [
                {'key':'foo', 'value':'bar'},
                {'key':'bar', 'value':'foo'}
            ]
    }
};
pm.sendRequest(reqObj, (error, response) => {
    if (error) throw new Error(error);
    console.log(response.json());
});

Postman will add the headers for โ€œmultipart/form-dataโ€ :slightly_smiling_face:

3 Likes

@bpricilla Thanks for your reply.

Unfortunately, the provided solution did not work as there was an error (API upload did not contain a file part).

Did you find a solution?

The format looks like that:

  body: {
    mode: 'formdata', formdata: [
      {key: 'foo', value: 'foo'},
      {key: 'bar', value: 'bar'}
    ]
  }

There are additional optional props you can pass like so:

  body: {
    mode: 'formdata', formdata: [
      {key: 'foo', value: 'foo', disabled: false, description: {content:"", type:"text/plain"}},
      {key: 'bar', value: 'bar', disabled: false, description: {content:"", type:"text/plain"}}
    ]
  }