Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] string payload is not json encoded #24

Open
damianobarbati opened this issue Dec 27, 2019 · 2 comments
Open

[bug] string payload is not json encoded #24

damianobarbati opened this issue Dec 27, 2019 · 2 comments

Comments

@damianobarbati
Copy link

The following results in a payload hola and not the json "hola".

import http from 'http';
import koa from 'koa';
import json from 'koa-json';

const app = new koa();
app.use(json());

app.use(ctx => ctx.body = 'hola');

http.createServer(app.callback()).listen(1234);

Current workaround:

app.use(async (ctx, next) => {
    await next();

    if (typeof ctx.body === 'string')
        ctx.body = `"${ctx.body}"`;
});
@jonathanong
Copy link
Member

why would you send a string JSON-encoded?

@damianobarbati
Copy link
Author

@jonathanong for consistency:

  • the whole react application is expecting json responses
  • the api documentation is documenting json responses (why having to document an exception?)
  • the artillery load testing scripts are expecting json responses (why having to put exception for 1 endpoint returning a string?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants