今天遇到 HPE_INVALID_CONSTANT 錯誤.
1. 再現
今天下午調用同事的接口, 莫名出現該異常.一直在找原因.
以下是http.get 代碼.. 沒發現問題,且該方法調用其他接口沒問題.
http.get(_options.url, function (res) { var pageData = ""; res.setEncoding(_options.encoding); res.on('error', function (err) { console.log(err); }); res.on('data', function (chunk) { pageData += chunk; }); res.on('end', function () { var content = pageData; callback(content); }); }).on('error', function (e) { console.log(e.message); callback(null); });
2. 找原因
nodejs 相關資料中文的偏少, 一直在google. 看的是滿頭霧水.
最后確定原因在於 同事的接口.
3. 解決
我比較了異常接口 和 正常接口, 才明白了為什么了.
以下是正常接口的 response 頭信息
Connection:keep-alive Content-Encoding:gzip Content-Type:text/xml Date:Thu, 08 May 2014 14:43:50 GMT Server:duomi/1.0.8 Transfer-Encoding:chunked
異常接口的 response 頭信息
Connection:keep-alive Content-Encoding:gzip Content-Type:application/json Date:Thu, 08 May 2014 14:37:47 GMT Server:duomi/1.0.8 Transfer-Encoding:chunked
最后看來確實是 Content-Type 屬性作祟!
看來http請求接口如果 Content-Type 是json 會自動序列化成 json 對象??