muratcorlu / connect-api-mocker

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

Can't seem to get anything from request.body with POST #64

Closed didymu5 closed 3 years ago

didymu5 commented 3 years ago

Server.js is as follows

app.use(
  '/api',
  apiMocker('api', {
    target: 'api',
    bodyParser: {
      type: 'urlencoded',
      options: { extended: true }
    },    
    verbose: ({ req, filePath, fileType }) =>
      console.log(
        `Mocking endpoint ${req.originalUrl} using ${filePath}.${fileType}.`
      )
  })
)

POST.js is as follows

module.exports = function (request, response) {
console.log(request.body)
return   response.sendFile('229110161010.json', { root: __dirname })

}

The log I get is is just and empty {}

How do I access the data in the body on my post calls?

muratcorlu commented 3 years ago

Can you send an example of your request object and request headers? Do you add "content-type: application/json" as request header?

didymu5 commented 3 years ago

Request:

POST /api/cart/item?envlbl=etc HTTP/1.1 Host: localhost:8080 Connection: keep-alive Content-Length: 0 sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90" X-pharmacyId: undefined X-IBM-client-Id: 5861ea12-91b0-4c90-a997-9d0602ad85fc sec-ch-ua-mobile: ?0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 X-env: dev3 Content-Type: application/json Accept: / Sec-Fetch-Site: same-site Sec-Fetch-Mode: cors Sec-Fetch-Dest: empty Referer: http://localhost:3000/ Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Cookie: ...

RES: HTTP/1.1 200 OK X-Powered-By: Express Accept-Ranges: bytes Cache-Control: public, max-age=0 Last-Modified: Tue, 13 Apr 2021 05:33:44 GMT ETag: W/"375-178c9b8a2ec" Content-Type: application/json; charset=UTF-8 Content-Length: 885 Date: Thu, 15 Apr 2021 23:10:59 GMT Connection: keep-alive Keep-Alive: timeout=5

muratcorlu commented 3 years ago

Request headers say  Content-Length: 0. So I think body is empty.

didymu5 commented 3 years ago

thanks @muratcorlu I hadn't realized I wasn't correctly using fetch function.