koajs / bodyparser

Koa body parsing middleware
MIT License
1.31k stars 116 forks source link

POST json body isn't set on `ctx.request.body` #59

Closed ralyodio closed 7 years ago

ralyodio commented 7 years ago

When I POST to my endpoint with a JSON body its not showing up on the server:

{
    "queries": ["pizza", "javascript"]
}

I get undefined for ctx.request.body

sending as form data seems to be ok for non-json objects. but I want to post json.

I've tried posting with both application/javascript and application/json neither work. (i'm using "koa-bodyparser": "^3.2.0" with "koa": "^2.0.0",)

Here is relevant app.js:

app
  .use(bodyParser())
  .use(cors({
    methods: ['GET', 'PUT', 'POST', 'PATCH', 'DELETE']
  }));

middleware(app);
routes(app);
ralyodio commented 7 years ago

My fault. Didn't define the content-type from Postman for "raw":

https://i.imgur.com/tKyEVnA.png

The default "text" was override my manual headers.

liuhanqu commented 7 years ago

i have meet with it too. u should set request header correctly which means 'Content-Type' is equal to 'application/json'.

elkarouani commented 3 years ago

I added the content-type header, but it didn't work

zedfight commented 2 years ago

i have meet too, i add koa-bodyparser

AlexJubs commented 2 years ago

I have the exact same issue. I'm using koa-body, and my request has the header content-type: application/json, yet the ctx.request.body is empty...