json
json copied to clipboard
[bug] string payload is not json encoded
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}"`;
});
why would you send a string JSON-encoded?
@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?)