Search code examples
ember.jsbody-parserember-cli-mirage

Ember.js: use body-parser library in mocking backend with mirage


I'm developing Ember.js application that authenticates against OAuth2 endpoint.

In order to mock this endpoint, I use ember-cli-mirage that needs to parse HTTP POST having content-type of x-www-form-urlencoded.

I decided to use body-parser npm package in order to parse the body of the request.

Currently I have this code in my config.js:

  var urlencodedParser = BodyParser.urlencoded({ extended: false });

  this.post('/login', (schema, request) => {
  });

I know that request.requestBody contains the data I'd like to parse, but I just can't find the correct way to make use of 'urlencodedParser` in order to parse this data.

Help will be appreciated.


Solution

  • const requestPayload = JSON.parse(request.requestBody);
    

    Headers can be found in request.requestHeaders. You don't need 3rd-party library for this.