question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

8reactions
liuhanqucommented, May 12, 2017

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

2reactions
ralyodiocommented, Feb 4, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

I can't access my request.body object in Koa JS (I am using ...
To access your body object, try the following: const body = ctx.request.body;. Secondly: because in one of the comments there was a note,...
Read more >
koa-body - npm
A Koa body parser middleware. Supports multipart, urlencoded and JSON request bodies.. Latest version: 6.0.1, last published: 2 months ago.
Read more >
Context - Fiber
The Ctx struct represents the Context which hold the HTTP request and response. It has methods for the request query string, parameters, body,...
Read more >
5.x API - Express.js
A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body ), or an empty...
Read more >
koa.Request.body JavaScript and Node.js code examples
创建评论 router.post('/reply', async (ctx) => { // 通过验证器校验参数是否通过 console.log(ctx.request.body) const v = await new ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found