How to return value from json-server on post request?

Viewed 238

The issue

  • GET /api/get-request works correctly, and `{ x: 'x' } is returned to the frontend
  • POST /api/post-request works too but returns empty body

How I run json-server:

json-server src/configs/api/mocks/json-server-generator.js --routes src/configs/api/mocks/json-server-routes.json --watch"

json-server-routes.json

{
  "/api/getrequest": "/getrequest",
  "/api/postrequest": "/postrequest",
 
}

json-server-generator.js

module.exports = () => {
   return {
      getrequest: { x: 'x' },
      postrequest: { y: 'y' }
   }
}

Requests are made via fetch:

fetch(url, { 
   method,
   headers: {
      'content-type': 'application/json'
    },
   body: JSON.stringify(payload)
})
0 Answers
Related