设置 content-type header 和 restify 结果 application/octet-stream
Setting content-type header with restify results in application/octet-stream
我正在尝试 restify,虽然我对 Express 更满意,但到目前为止它非常棒。我正在尝试在响应中设置内容类型 header,如下所示:
server.get('/xml', function(req, res) {
res.setHeader('content-type', 'application/xml');
// res.header('content-type', 'application/xml'); // tried this too
// res.contentType = "application/xml"; // tried this too
res.send("<root><test>stuff</test></root>");
});
但我得到的回复却是 application/octet-stream
。
我也尝试了 res.contentType('application/xml')
,但实际上引发了错误 ("Object HTTP/1.1 200 OK\ has no method 'contentType'"
)。
在响应中将内容类型 header 设置为 xml 的正确方法是什么?
更新:
当我执行 console.log(res.contentType);
时,它实际上输出 application/xml
。为什么它不在响应 headers?
中
卷曲片段:
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /xml?params=1,2,3 HTTP/1.1
> User-Agent: curl/7.39.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/octet-stream
< Content-Length: 8995
< Date: Mon, 23 Feb 2015 20:20:14 GMT
< Connection: keep-alive
<
<body goes here>
When I do console.log(res.contentType); it actually outputs application/xml. Why is it not in the response headers?
您所做的就是在 res
对象上设置 属性。因为这是 JavaScript,它工作正常,你可以读回 属性 值,但这不是节点核心或 restify 的正确 API,所以它被其他所有东西忽略比你的代码。
根据您链接到的 restify 文档,我认为您的 res.header("Content-Type", "application/xml");
是正确的。因此,我的直觉是您的工具可能会误导您。您确定您在响应中看到了原始值(许多开发人员工具将毫无帮助 "prettify" 或以其他方式欺骗您)并且您正在按照您真正认为的方式行事吗? curl -v
或 httpie --headers
的输出会有所帮助。
结果失败的原因是我没有使用 Restify
的响应处理程序发送响应;它默认为本机 Node.js 处理程序。
我在哪里做的:
res.send(js2xmlparser("search", obj));
我应该这样做的:
res.end(js2xmlparser("search", o));
// ^ end, not send!
可以通过在创建服务器时向服务器实例添加格式化程序来 return application/xml:
var server = restify.createServer( {
formatters: {
'application/xml' : function( req, res, body, cb ) {
if (body instanceof Error)
return body.stack;
if (Buffer.isBuffer(body))
return cb(null, body.toString('base64'));
return cb(null, body);
}
}
});
然后在代码的某些部分:
res.setHeader('content-type', 'application/xml');
res.send('<xml>xyz</xml>');
您可以使用 sendRaw 而不是 发送 来发送 XML 响应。 sendRaw 方法根本不使用任何格式化程序(如果需要,您应该预先格式化您的响应)。请参阅下面的示例:
server.get('/xml', function(req, res, next) {
res.setHeader('content-type', 'application/xml');
res.sendRaw('<xml>xyz</xml>');
next();
});
我正在尝试 restify,虽然我对 Express 更满意,但到目前为止它非常棒。我正在尝试在响应中设置内容类型 header,如下所示:
server.get('/xml', function(req, res) {
res.setHeader('content-type', 'application/xml');
// res.header('content-type', 'application/xml'); // tried this too
// res.contentType = "application/xml"; // tried this too
res.send("<root><test>stuff</test></root>");
});
但我得到的回复却是 application/octet-stream
。
我也尝试了 res.contentType('application/xml')
,但实际上引发了错误 ("Object HTTP/1.1 200 OK\ has no method 'contentType'"
)。
在响应中将内容类型 header 设置为 xml 的正确方法是什么?
更新:
当我执行 console.log(res.contentType);
时,它实际上输出 application/xml
。为什么它不在响应 headers?
卷曲片段:
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /xml?params=1,2,3 HTTP/1.1
> User-Agent: curl/7.39.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/octet-stream
< Content-Length: 8995
< Date: Mon, 23 Feb 2015 20:20:14 GMT
< Connection: keep-alive
<
<body goes here>
When I do console.log(res.contentType); it actually outputs application/xml. Why is it not in the response headers?
您所做的就是在 res
对象上设置 属性。因为这是 JavaScript,它工作正常,你可以读回 属性 值,但这不是节点核心或 restify 的正确 API,所以它被其他所有东西忽略比你的代码。
根据您链接到的 restify 文档,我认为您的 res.header("Content-Type", "application/xml");
是正确的。因此,我的直觉是您的工具可能会误导您。您确定您在响应中看到了原始值(许多开发人员工具将毫无帮助 "prettify" 或以其他方式欺骗您)并且您正在按照您真正认为的方式行事吗? curl -v
或 httpie --headers
的输出会有所帮助。
结果失败的原因是我没有使用 Restify
的响应处理程序发送响应;它默认为本机 Node.js 处理程序。
我在哪里做的:
res.send(js2xmlparser("search", obj));
我应该这样做的:
res.end(js2xmlparser("search", o));
// ^ end, not send!
可以通过在创建服务器时向服务器实例添加格式化程序来 return application/xml:
var server = restify.createServer( {
formatters: {
'application/xml' : function( req, res, body, cb ) {
if (body instanceof Error)
return body.stack;
if (Buffer.isBuffer(body))
return cb(null, body.toString('base64'));
return cb(null, body);
}
}
});
然后在代码的某些部分:
res.setHeader('content-type', 'application/xml');
res.send('<xml>xyz</xml>');
您可以使用 sendRaw 而不是 发送 来发送 XML 响应。 sendRaw 方法根本不使用任何格式化程序(如果需要,您应该预先格式化您的响应)。请参阅下面的示例:
server.get('/xml', function(req, res, next) {
res.setHeader('content-type', 'application/xml');
res.sendRaw('<xml>xyz</xml>');
next();
});