如果从服务器(meteor)调用GET响应内容为空,

所以我想要做的是在Meteor服务器端使用标准的HTTP包parsing这个基于XML的RSS源。 混淆的是,如果从服务器调用,我收到一个空的内容响应,但如果我从客户端调用相同的端点,那么我会收到一个正常的XML内容。 我尝试从使用Meteor.call的客户端调用服务器方法,也试图用meteorshell调用它。 同样的结果

这是Meteor方法:

readRssFeed: function() { try { HTTP.get("http://agenda.regiolive.ch/feed/", {}, function(error, response) { console.log(response) //parse xml }); } catch (error) { console.log(error); } }, 

答复是:

 I20170514-15:20:06.474(3)? { statusCode: 200, I20170514-15:20:06.474(3)? content: '', I20170514-15:20:06.474(3)? headers: I20170514-15:20:06.474(3)? { date: 'Sun, 14 May 2017 12:12:04 GMT', I20170514-15:20:06.474(3)? server: 'Apache-Coyote/1.1', I20170514-15:20:06.474(3)? 'access-control-allow-origin': '*', I20170514-15:20:06.474(3)? 'content-type': 'application/xml;charset=UTF-8', I20170514-15:20:06.474(3)? 'content-length': '0', I20170514-15:20:06.474(3)? 'set-cookie': I20170514-15:20:06.474(3)? [ 'cfid=9d25f146-1fca-42d2-927e-88fb9e458bfa;Path=/;Expires=Mon, 13-May-2047 20:03:34 GMT;HTTPOnly', I20170514-15:20:06.475(3)? 'cftoken=0;Path=/;Expires=Mon, 13-May-2047 20:03:34 GMT;HTTPOnly', I20170514-15:20:06.475(3)? 'CF_CLIENT_AGENDA_REGIOLIVE_CH1104_LV=1494764405749;Path=/;Expires=Wed, 24-May-2017 12:20:06 GMT', I20170514-15:20:06.475(3)? 'CF_CLIENT_AGENDA_REGIOLIVE_CH1104_TC=1494764405749;Path=/;Expires=Wed, 24-May-2017 12:20:06 GMT', I20170514-15:20:06.475(3)? 'CF_CLIENT_AGENDA_REGIOLIVE_CH1104_HC=2;Path=/;Expires=Wed, 24-May-2017 12:20:06 GMT' ], I20170514-15:20:06.475(3)? 'keep-alive': 'timeout=5, max=100', I20170514-15:20:06.475(3)? connection: 'Keep-Alive' }, I20170514-15:20:06.475(3)? data: null } 

如果我在客户端执行相同的代码,我会收到XML内容的预期响应

 { "statusCode": 200, "content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<feed xmlns=\"http://www.w3.org/2005/Atom\"\nxmlns:ev=\"http://purl.org/rss/1.0/modules/event/\"\nxmlns:geo=\"http://www.w3.org/2003/01/g.....>, "headers": { "content-type": "application/xml;charset=UTF-8" }, "data": null } 

我也尝试使用Node.js样式的HTTP请求。 同样的结果

如果您在浏览器中直接导航到http://agenda.regiolive.ch/feed/ ,则会得到一个gzip压缩的响应:

 HTTP/1.1 200 OK Date: Sun, 14 May 2017 13:33:08 GMT Server: Apache-Coyote/1.1 Access-Control-Allow-Origin: * Content-Type: application/xml;charset=UTF-8 Content-Encoding: gzip ^^^^ 

鉴于为了表明他们接受gzip的响应,浏览器发送以下请求标头:

 Accept-Encoding: gzip, deflate 

…那么看起来,如果http://agenda.regiolive.ch/feed/ 没有得到这个请求标题,它根本就不会发送响应主体; 但是如果确实得到了头文件,那么它会按照预期的那样发送XML,但是会被压缩。

所以你可以尝试让你的代码也发送一个Accept-Encoding: gzip, deflate的请求Accept-Encoding: gzip, deflate ,并且你会得到一个与XML一样的响应主体,但是是gzip。

但是你并不需要一个gzip的响应,所以你可以尝试的是,让你的代码添加一个Accept-Encoding: identity请求标头到请求。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding

identity
指示身份function(即不压缩,也不修改)。

发送Accept-Encoding: identity使用curl Accept-Encoding: identity按预期得到(非空)XML响应:

 $ curl -s -H "Accept-Encoding: identity" http://agenda.regiolive.ch/feed/ | head <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:ev="http://purl.org/rss/1.0/modules/event/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> <author> <name>Regiolive Agenda</name> </author> <title>Regiolive Agenda</title> <id>http://regiolive.ch/</id> <updated>2017-05-14T17:00:00Z</updated> 

所以,如果你让你的代码添加Accept-Encoding: identity ,你应该得到你需要的XML。