因此,可以尝试获取以下JSON对象:
$ curl -i -X GET http://echo.jsontest.com/key/value/anotherKey/anotherValue
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=ISO-8859-1
Date: Wed, 30 Oct 2013 22:19:10 GMT
Server: Google Frontend
Cache-Control: private
Alternate-Protocol: 80:quic,80:quic
Transfer-Encoding: chunked
{
"anotherKey": "anotherValue",
"key": "value"
}
$
Run Code Online (Sandbox Code Playgroud)
有没有办法在使用node或express的服务器的响应中生成完全相同的主体?显然,可以设置标头并指示响应的内容类型将是"application/json",但是有不同的方式来编写/发送对象.我经常使用的那个是使用以下形式的命令:
response.write(JSON.stringify(anObject));
Run Code Online (Sandbox Code Playgroud)
然而,这有两点可以说是好像是"问题":
另一个想法是使用命令:
response.send(anObject);
Run Code Online (Sandbox Code Playgroud)
这似乎是基于curl的输出发送一个JSON对象,类似于上面的第一个例子.但是,当在终端上再次使用卷曲时,在身体的末端没有新的线条字符.那么,如何使用node或node/express在最后附加一个新行字符来实际记下这样的内容?