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