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.

Koa always overwrite `Content-Type` for JSON responses

See original GitHub issue
const Koa = require('koa');
const app = new Koa();
app.use(ctx => {
  ctx.set('Content-Type', 'application/vnd.myapi.v1+json');
  ctx.body = {message: 'hello'};
});
app.listen(3000);

When running this code I aways get Content-Type: application/json; charset=utf-8.

I can make it work if I write the header after setting the body. It only affects json responses. I’m not sure if that’s a bug, but I couldn’t find any information about that behavior in documentation.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
jonathanongcommented, Jan 31, 2018

seems like a bug where it overwrites the content type when the content type is set and has +json in it.

4reactions
uzilcommented, Feb 2, 2018

Check if body has value of type object, and if the header contains +json preserve the user given header. Is this the desired implementation of fix for this bug?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Content-type header is always the same while testing koa ...
When testing routes with supertest I face a problem, that content-type response header is always application/json; charset=utf-8 . const app = ...
Read more >
Receiving Requests with Koa
Koa is a small framework that lets us create backend apps that run on the Node.js platform. In this article, we'll look at...
Read more >
Koa - next generation web framework for node.js
A Koa Context encapsulates node's request and response objects into a single object ... 'text/html' // When Content-Type is application/json ctx.is('json', ...
Read more >
Koa.js - Quick Guide
Step 3 − Now we have our package.json file set up, we'll install Koa. To install Koa and add it in ... Get...
Read more >
Express 5.x - API Reference
Returns middleware that only parses JSON and only looks at requests where the Content-Type header matches the type option. This parser accepts any...
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