json icon indicating copy to clipboard operation
json copied to clipboard

[bug] string payload is not json encoded

Open damianobarbati opened this issue 5 years ago • 2 comments

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}"`;
});

damianobarbati avatar Dec 27 '19 18:12 damianobarbati

why would you send a string JSON-encoded?

jonathanong avatar Apr 28 '20 05:04 jonathanong

@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?)

damianobarbati avatar Mar 14 '23 12:03 damianobarbati