form-auto-content icon indicating copy to clipboard operation
form-auto-content copied to clipboard

Cannot add JWT Bearer token to a form

Open gt-mj-1107 opened this issue 2 years ago • 1 comments

Form options not taking into consideration, options passed Sample Code:

const option = {
        headers: {
          Authorization: `Bearer ${jwtToken}`,
        },
        payload: {
          'content-type': 'multipart/form-data',
        },
      }

const form = formAutoContent(
        {
          file: fs.createReadStream(`./test/fixtures/test.txt`),
        },
        option,
      )

Rest API call never will get JWT as it's not passed to request

gt-mj-1107 avatar Jun 26 '23 11:06 gt-mj-1107

Wrong usage of the API:

const form = formAutoContent(
        {
          file: fs.createReadStream(`./test/fixtures/test.txt`),
        })

const myHeaders = {
  ...form.headers, // the content-type is already added automatically
  Authorization: `Bearer ${jwtToken}`
}

const thePayloadToSubmit = form.payload

The option object lets you to change the output field names:

const form = formAutoContent(
        {
          file: fs.createReadStream(`./test/fixtures/test.txt`),
        }, { payload: 'FOOO', headers: 'BAR' )

const myHeaders = {
  ...form.BAR, // the content-type is already added automatically
  Authorization: `Bearer ${jwtToken}`
}

const thePayloadToSubmit = form.FOOO

I think we may trigger an error when the input options does not contain a valid string value (as in this case, an object was provided)

Eomm avatar Jun 26 '23 13:06 Eomm