我想使用 eBay API 获取SessionId
。我正在使用 Node.js 作为后端。在响应中,我收到此错误:
输入传输已终止,因为您的请求超时。
为了获得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 创建请求对象时没有 body 选项。
在请求中设置正文的正确方法是在调用 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();