Http响应机构有来自Dart Aqueduct服务器的引号

问题描述 投票:0回答:1

从这样的Aqueduct控制器返回响应时

return Response.ok('hello');

响应的主体周围有引号:

"hello"

当我返回这样的JSON字符串时,同样的事情:

return Response.ok('{"token":"$token"}');

我明白了:

"{\"token\":\"eyJhbG...soOFY8\"}"

这搞乱了客户端的JSON解析。

有没有办法不发送引号?

json dart httpresponse aqueduct
1个回答
0
投票

响应的默认内容类型已经是JSON。如果要发送平面文本,则需要将内容类型设置为纯文本。

// import 'dart:io';

return Response.ok('hello')..contentType = ContentType.text;

响应机构将是

hello

要发送JSON,只需发送一个Map而不是自己将其转换为字符串:

return Response.ok({'token':token});

这将给出一个响应体

{"token":"eyJhbGc...vCxdE"}

See also

Credit

感谢Joe ConwayAqueduct Slack channel上帮助解决这个问题。我在这里添加解决方案作为问答,以便其他人可以更容易地找到它。

© www.soinside.com 2019 - 2024. All rights reserved.