Modifying the FormData before sending into the request

Hi guys,

I need to find a way to modify the FormData value before sending it to the request.

I have tried several ways, but most of them only works with JSON values.

Here are my attempts:

const dataObj = {
  'form_id': 8,
  'item_meta[59]': 'postmanemail@gmail.com',
  'item_meta[66]': ['Subscribe to <span>Benchmark</span> Newsletter'],
};

const d = Object.keys(dataObj).map(key => {
  return {
    key,
    value: dataObj[key],
    disabled: false,
    description: {content:"", type:"text/plain"}
  }
})

First attempt: Trying to add body object to a variable before sending it:

pm.variables.set('formidableEmail', JSON.stringify({
  mode: 'formdata',
  formdata: d
}))
// Didn't work T.T

Second attempt: Modifying the body

pm.request.body.mode = 'formdata'
pm.request.body.formdata = dataObj
// Didn't work either T.T

Most promising attempt: sending the request from the pre-request script:

pm.sendRequest({
  url: `url_of_the_service`,
  method: 'POST',
  header: {
    'Content-Type': 'multipart/form-data',
  },
  body: {
    mode: 'formdata',
    formdata: d
  }
}, function (err, response) {
  if (err) throw new Error(err)
  console.log(response)
})

That last attempt has been the most promising one for me, but it has some problems:

  • How to avoid sending the main request? Because it’s sending two requests: one for the main request and a second one for the pre-request script.
  • How can I pass the result so I can test it on the β€œTests” tab?
  • How can I read the response for future debugging?

If there is another approach I could take, I will be more than happy to test it.

Kind regards,
Luis.