当像这样从 Aqueduct 控制器返回响应时
return Response.ok('hello');
响应的正文周围有引号:
"hello"
当我返回这样的 JSON 字符串时,同样的事情:
return Response.ok('{"token":"$token"}');
我明白了:
"{\"token\":\"eyJhbG...soOFY8\"}"
这弄乱了客户端的 JSON 解析。
有没有办法不发送引号?
当像这样从 Aqueduct 控制器返回响应时
return Response.ok('hello');
响应的正文周围有引号:
"hello"
当我返回这样的 JSON 字符串时,同样的事情:
return Response.ok('{"token":"$token"}');
我明白了:
"{\"token\":\"eyJhbG...soOFY8\"}"
这弄乱了客户端的 JSON 解析。
有没有办法不发送引号?
响应的默认 ContentType 已经是 JSON。如果要发送平面文本,则需要将内容类型设置为纯文本。
// import 'dart:io';
return Response.ok('hello')..contentType = ContentType.text;
响应正文将是
hello
要发送 JSON,只需发送 Map 而不是自己将其转换为字符串:
return Response.ok({'token':token});
这将给出一个响应体
{"token":"eyJhbGc...vCxdE"}
感谢Aqueduct Slack 频道的Joe Conway帮助解决此问题。我在这里添加解决方案作为问答,以便其他人可以更轻松地找到它。