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



所以我要做的是使用标准的HTTP包在Meteor服务器端解析这个基于XML的RSS提要。令人困惑的是,如果从服务器调用,我会收到一个空内容作为响应,但是如果我从客户端调用相同的端点,那么我会收到一个正常的 XML 内容。我尝试使用 Meteor.call 从客户端调用服务器方法,也尝试使用 meteor shell 调用它。相同的结果

这是流星方法:

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的请求,并且您也会按预期获得带有 XML 的响应正文,但已压缩。

但是你并不真正需要一个压缩的响应,所以你可以尝试的是,让你的代码改为向请求添加一个Accept-Encoding: identity请求标头。

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


      identity表示恒等函数(即不压缩,也不修改)。

发送带有curlAccept-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。

最新更新