muratcorlu / connect-api-mocker

Connect middleware that creates mocks for REST APIs
Apache License 2.0
271 stars 18 forks source link

Parse FormData request #60

Closed chr1s1k closed 10 months ago

chr1s1k commented 3 years ago

Hi there,

is it possible to read and parse data from a request that are being sent as FormData?

const formData = new FormData()
formData.append('param1', 'value1')
formData.append('param2', 'value2')

fetch('/api/mock-url', {
  method: 'POST',
  body: formData,
})

And then in api/mock-url/POST.js

module.exports = function (request, response) {
  // ???
}
muratcorlu commented 3 years ago

connect-api-mocker uses body-parser for parsing request bodies. And it's possible to set the type of request body for body-parser as follows:

apiMocker('/text', {
  target: 'test/mocks',
  bodyParser: {
    type: 'urlencoded',
    options: { extended: true }
  }
})

For other options of bodyParser.urlencoded you can check document: https://github.com/expressjs/body-parser#bodyparserurlencodedoptions

Hopefully that will help.