易趣超时错误

我想要使​​用eBay API获取SessionId。 我使用Node.js作为后端。 在响应中我得到这个错误:“input传输已经终止,因为你的请求超时”。

要获得sessionId我正在使用以下方法。

var xml = '<?xml version="1.0" encoding="utf-8"?>'+ '<GetSessionIDRequest xmlns="urn:ebay:apis:eBLBaseComponents">'+ '<RuName>MyRuname</RuName>'+ '</GetSessionIDRequest>'; var options = { host: "api.sandbox.ebay.com", path: '/ws/api.dll', method: "POST", body: xml, headers: { 'X-EBAY-API-APP-NAME': 'my app id', 'X-EBAY-API-DEV-NAME': 'my dev id', 'X-EBAY-API-CERT-NAME': 'my cert id', 'X-EBAY-API-COMPATIBILITY-LEVEL': '557', 'X-EBAY-API-CALL-NAME': 'GetSessionID', 'X-EBAY-API-SITEID':'203', 'Content-Type' : 'text/xml', 'Content-Length':xml.length } }; var req = https.request(options, function (res) { console.log("statusCode: ", res.statusCode); console.log("headers: ", res.headers); res.on('data', function (d) { process.stdout.write(d); }); }); req.end(); req.on('error', function (e) { console.error('error=======', e); }); 

如果您发送一个空的POST正文,则会发生该错误。 如果您查看nodejs 文档,您将看到使用https.request创build请求对象时没有主体选项。

在请求中设置正文的正确方法是在调用req.end之前调用req.write方法

 var options = { host: "api.sandbox.ebay.com", path: '/ws/api.dll', method: "POST", headers: { 'X-EBAY-API-APP-NAME': 'my app id', 'X-EBAY-API-DEV-NAME': 'my dev id', 'X-EBAY-API-CERT-NAME': 'my cert id', 'X-EBAY-API-COMPATIBILITY-LEVEL': '557', 'X-EBAY-API-CALL-NAME': 'GetSessionID', 'X-EBAY-API-SITEID':'203', 'Content-Type' : 'text/xml', 'Content-Length':xml.length } }; var req = https.request(options, function (res) { console.log("statusCode: ", res.statusCode); console.log("headers: ", res.headers); res.on('data', function (d) { process.stdout.write(d); }); }); req.on('error', function (e) { console.error('error=======', e); }); req.write(xml); req.end();