2016-01-20 60 views
1

我正在按照Meteor Chef的教程进行操作。我正在使用它来创建一个Meteor的路由/ API,它将接收来自eBay的通知。但我遇到的问题是,我从eBay收到的request.body是一个空的对象。我可以看到它发送给我的头文件,在请求中发送的头文件中,它有soapaction:'“http // developer.ebay.com/notification/AskSellerQuestion''。 (我通过询问一个问题来测试它,所以它似乎工作)如何从eBay通知中获取数据

在eBay Notifications的请求中,我是否可以找到具有关于通知信息的XML?还是我以错误的方式去解决它?我发现eBay的文档不是很有帮助。

我被请求代码:

这是我api.js

API = { 

    authentication: function(apiKey) { 
     // Authenticate with the app's API Key 
     var getKey = Meteor.settings.auroraAPIKey; 
     if (apiKey === getKey) { 
     return true; 
     } else { 
     return false; 
     } 

    }, 
    connection: function(request, key) { 
     var getRequestContents = API.utility.getRequestContents(request), 
      //apiKey    = getRequestContents.api_key, 
      validUser   = API.authentication(key); 

     if (validUser) { 
     //delete getRequestContents.api_key; 
     // return data 
     // This object is refered to as 'connection.data' 
     return { owner: 'Aurora', data: getRequestContents }; 
     } else { 
     return { error: 401, message: "Invalid API key." }; 
     } 
    }, 
    handleRequest: function(context, resource, method, key) { 
     var connection = API.connection(context.request, key); 
     if (!connection.error) { 
     API.methods[ resource ][ method ](context, connection); 
     } else { 
     API.utility.response(context, 401, connection); 
     } 
    }, 

    methods: { 
     ebay: { 
     GET: function(context, connection) {}, 
     POST: function(context, connection) { 

      // HERE: is where I want to access the request.body but it is empty 

      console.log(connection.data, context.request); 
      // Response of 200 is required for eBay Notifications 
      API.utility.response(context, 200, connection.data); 

      let soapaction = context.request.headers.soapaction; 
      let split = soapaction.split('/notification/'); 
      let notification = split[ 1 ]; 

      EbayNotifications.insert(
       { 
        createdAt: new Date(), 
        soapaction: soapaction, 
        notification: notification 
       } 
      ); 
     }, 
     PUT: function(context, connection) {}, 
     DELETE: function(context, connection) {} 
     } 
    }, 

    utility: { 
     getRequestContents: function(request) { 
     switch(request.method) { 
      case "GET": 
       return request.query; 
      case "POST": 
      case "PUT": 
      case "DELETE": 
       return request.body; 
     } 
     }, 
     hasData: function(data) { 
     return Object.keys(data).length > 0 ? true : false; 
     }, 
     response: function(context, statusCode, data) { 
     context.response.setHeader('Content-Type', 'application/json'); 
     context.response.statusCode = statusCode; 
     context.response.end(JSON.stringify(data)); 
     }, 
     validate: function(data, pattern) { 
     return Match.test(data, pattern); 
     } 
    } 

}; 

这是我的路线:

Router.route('/api/ebay/:key', function() { 
    this.response.setHeader('Access-Control-Allow-Origin', '*'); 
    // https://api.ebay.com/ws/api.dll 

    if (this.request.method === "OPTIONS") { 
     this.response.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); 
     this.response.setHeader('Access-Control-Allow-Methods', 'POST, PUT, GET, DELETE, OPTIONS'); 
     this.response.end('Set OPTIONS.'); 
    } else { 
     API.handleRequest(this, 'ebay', this.request.method, this.params.key); 
    } 
}, { where: 'server' }); 

这成功接收eBay平台的通知。我只是不确定在哪里访问XML,因为request.body是空白的。我无休止地搜索了谷歌我需要的信息。易趣有文档:SetNotificationPreferencesWorking With Platform NotificationsGetting Notifications我已经签出,但似乎没有帮助我的情况。

编辑再次:这是我在请求我得到当我登录它:

{} { _readableState: 
    { highWaterMark: 16384, 
    buffer: [], 
    length: 0, 
    pipes: null, 
    pipesCount: 0, 
    flowing: false, 
    ended: false, 
    endEmitted: false, 
    reading: false, 
    calledRead: false, 
    sync: true, 
    needReadable: false, 
    emittedReadable: false, 
    readableListening: false, 
    objectMode: false, 
    defaultEncoding: 'utf8', 
    ranOut: false, 
    awaitDrain: 0, 
    readingMore: false, 
    decoder: null, 
    encoding: null }, 
    readable: true, 
    domain: null, 
    _events: { close: [Function] }, 
    _maxListeners: 10, 
    socket: 
    { _connecting: false, 
    _handle: 
     { fd: 32, 
     writeQueueSize: 0, 
     owner: [Circular], 
     onread: [Function: onread], 
     reading: true }, 
    _readableState: 
     { highWaterMark: 16384, 
     buffer: [], 
     length: 0, 
     pipes: null, 
     pipesCount: 0, 
     flowing: false, 
     ended: false, 
     endEmitted: false, 
     reading: true, 
     calledRead: true, 
     sync: false, 
     needReadable: true, 
     emittedReadable: false, 
     readableListening: false, 
     objectMode: false, 
     defaultEncoding: 'utf8', 
     ranOut: false, 
     awaitDrain: 0, 
     readingMore: false, 
     decoder: null, 
     encoding: null }, 
    readable: true, 
    domain: null, 
    _events: 
     { end: [Object], 
     finish: [Function: onSocketFinish], 
     _socketEnd: [Function: onSocketEnd], 
     drain: [Object], 
     timeout: [Function], 
     error: [Function], 
     close: [Object] }, 
    _maxListeners: 10, 
    _writableState: 
     { highWaterMark: 16384, 
     objectMode: false, 
     needDrain: false, 
     ending: false, 
     ended: false, 
     finished: false, 
     decodeStrings: false, 
     defaultEncoding: 'utf8', 
     length: 0, 
     writing: false, 
     sync: false, 
     bufferProcessing: false, 
     onwrite: [Function], 
     writecb: null, 
     writelen: 0, 
     buffer: [], 
     errorEmitted: false }, 
    writable: true, 
    allowHalfOpen: true, 
    onend: [Function], 
    destroyed: false, 
    bytesRead: 4987, 
    _bytesDispatched: 1231, 
    _pendingData: null, 
    _pendingEncoding: '', 
    server: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     _connections: 6, 
     connections: [Getter/Setter], 
     _handle: [Object], 
     _usingSlaves: false, 
     _slaves: [], 
     allowHalfOpen: true, 
     httpAllowHalfOpen: false, 
     timeout: 5000, 
     _connectionKey: '4:0.0.0.0:8080' }, 
    _idleTimeout: 120000, 
    _idleNext: 
     { _connecting: false, 
     _handle: [Object], 
     _readableState: [Object], 
     readable: true, 
     domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     _writableState: [Object], 
     writable: true, 
     allowHalfOpen: false, 
     onend: null, 
     destroyed: false, 
     bytesRead: 14384, 
     _bytesDispatched: 1798, 
     _pendingData: null, 
     _pendingEncoding: '', 
     _idleTimeout: 30000, 
     _idleNext: [Object], 
     _idlePrev: [Circular], 
     _idleStart: 1453475881306, 
     _monotonicStartTime: 898918969, 
     pipe: [Function], 
     addListener: [Function: addListener], 
     on: [Function: addListener], 
     pause: [Function], 
     resume: [Function], 
     read: [Function], 
     _consuming: true }, 
    _idlePrev: { _idleNext: [Circular], _idlePrev: [Object] }, 
    _idleStart: 1453475881568, 
    _monotonicStartTime: 898919231, 
    parser: 
     { _headers: [], 
     _url: '', 
     onHeaders: [Function: parserOnHeaders], 
     onHeadersComplete: [Function: parserOnHeadersComplete], 
     onBody: [Function: parserOnBody], 
     onMessageComplete: [Function: parserOnMessageComplete], 
     socket: [Circular], 
     incoming: [Circular], 
     maxHeaderPairs: 2000, 
     onIncoming: [Function] }, 
    ondata: [Function], 
    _paused: false, 
    _httpMessage: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: false, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     sendDate: true, 
     _headerSent: false, 
     _header: '', 
     _hasBody: true, 
     _trailer: '', 
     finished: false, 
     _hangupClose: false, 
     socket: [Circular], 
     connection: [Circular], 
     _headers: [Object], 
     _headerNames: [Object], 
     write: [Function], 
     end: [Function] } }, 
    connection: 
    { _connecting: false, 
    _handle: 
     { fd: 32, 
     writeQueueSize: 0, 
     owner: [Circular], 
     onread: [Function: onread], 
     reading: true }, 
    _readableState: 
     { highWaterMark: 16384, 
     buffer: [], 
     length: 0, 
     pipes: null, 
     pipesCount: 0, 
     flowing: false, 
     ended: false, 
     endEmitted: false, 
     reading: true, 
     calledRead: true, 
     sync: false, 
     needReadable: true, 
     emittedReadable: false, 
     readableListening: false, 
     objectMode: false, 
     defaultEncoding: 'utf8', 
     ranOut: false, 
     awaitDrain: 0, 
     readingMore: false, 
     decoder: null, 
     encoding: null }, 
    readable: true, 
    domain: null, 
    _events: 
     { end: [Object], 
     finish: [Function: onSocketFinish], 
     _socketEnd: [Function: onSocketEnd], 
     drain: [Object], 
     timeout: [Function], 
     error: [Function], 
     close: [Object] }, 
    _maxListeners: 10, 
    _writableState: 
     { highWaterMark: 16384, 
     objectMode: false, 
     needDrain: false, 
     ending: false, 
     ended: false, 
     finished: false, 
     decodeStrings: false, 
     defaultEncoding: 'utf8', 
     length: 0, 
     writing: false, 
     sync: false, 
     bufferProcessing: false, 
     onwrite: [Function], 
     writecb: null, 
     writelen: 0, 
     buffer: [], 
     errorEmitted: false }, 
    writable: true, 
    allowHalfOpen: true, 
    onend: [Function], 
    destroyed: false, 
    bytesRead: 4987, 
    _bytesDispatched: 1231, 
    _pendingData: null, 
    _pendingEncoding: '', 
    server: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     _connections: 6, 
     connections: [Getter/Setter], 
     _handle: [Object], 
     _usingSlaves: false, 
     _slaves: [], 
     allowHalfOpen: true, 
     httpAllowHalfOpen: false, 
     timeout: 5000, 
     _connectionKey: '4:0.0.0.0:8080' }, 
    _idleTimeout: 120000, 
    _idleNext: 
     { _connecting: false, 
     _handle: [Object], 
     _readableState: [Object], 
     readable: true, 
     domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     _writableState: [Object], 
     writable: true, 
     allowHalfOpen: false, 
     onend: null, 
     destroyed: false, 
     bytesRead: 14384, 
     _bytesDispatched: 1798, 
     _pendingData: null, 
     _pendingEncoding: '', 
     _idleTimeout: 30000, 
     _idleNext: [Object], 
     _idlePrev: [Circular], 
     _idleStart: 1453475881306, 
     _monotonicStartTime: 898918969, 
     pipe: [Function], 
     addListener: [Function: addListener], 
     on: [Function: addListener], 
     pause: [Function], 
     resume: [Function], 
     read: [Function], 
     _consuming: true }, 
    _idlePrev: { _idleNext: [Circular], _idlePrev: [Object] }, 
    _idleStart: 1453475881568, 
    _monotonicStartTime: 898919231, 
    parser: 
     { _headers: [], 
     _url: '', 
     onHeaders: [Function: parserOnHeaders], 
     onHeadersComplete: [Function: parserOnHeadersComplete], 
     onBody: [Function: parserOnBody], 
     onMessageComplete: [Function: parserOnMessageComplete], 
     socket: [Circular], 
     incoming: [Circular], 
     maxHeaderPairs: 2000, 
     onIncoming: [Function] }, 
    ondata: [Function], 
    _paused: false, 
    _httpMessage: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: false, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     sendDate: true, 
     _headerSent: false, 
     _header: '', 
     _hasBody: true, 
     _trailer: '', 
     finished: false, 
     _hangupClose: false, 
     socket: [Circular], 
     connection: [Circular], 
     _headers: [Object], 
     _headerNames: [Object], 
     write: [Function], 
     end: [Function] } }, 
    httpVersion: '1.1', 
    complete: false, 
    headers: 
    { host: '/*...omitted...*/', 
    'user-agent': 'eBayNioHttpClient 1.0', 
    'content-length': '2840', 
    'cache-control': 'max-age=86400', 
    connection: 'keep-alive', 
    'content-type': 'text/xml;charset=utf-8', 
    soapaction: '"http://developer.ebay.com/notification/AskSellerQuestion"', 
    via: '1.1 phx7b02c-718782 (squid)', 
    'x-forwarded-for': '/*...omitted...*/', 
    'x-forwarded-proto': 'http', 
    'x-mod-region': '/*...omitted...*/', 
    'accept-encoding': 'gzip' }, 
    trailers: {}, 
    _pendings: [], 
    _pendingIndex: 0, 
    url: '/api/ebay//*...omitted...*/', 
    method: 'POST', 
    statusCode: null, 
    client: 
    { _connecting: false, 
    _handle: 
     { fd: 32, 
     writeQueueSize: 0, 
     owner: [Circular], 
     onread: [Function: onread], 
     reading: true }, 
    _readableState: 
     { highWaterMark: 16384, 
     buffer: [], 
     length: 0, 
     pipes: null, 
     pipesCount: 0, 
     flowing: false, 
     ended: false, 
     endEmitted: false, 
     reading: true, 
     calledRead: true, 
     sync: false, 
     needReadable: true, 
     emittedReadable: false, 
     readableListening: false, 
     objectMode: false, 
     defaultEncoding: 'utf8', 
     ranOut: false, 
     awaitDrain: 0, 
     readingMore: false, 
     decoder: null, 
     encoding: null }, 
    readable: true, 
    domain: null, 
    _events: 
     { end: [Object], 
     finish: [Function: onSocketFinish], 
     _socketEnd: [Function: onSocketEnd], 
     drain: [Object], 
     timeout: [Function], 
     error: [Function], 
     close: [Object] }, 
    _maxListeners: 10, 
    _writableState: 
     { highWaterMark: 16384, 
     objectMode: false, 
     needDrain: false, 
     ending: false, 
     ended: false, 
     finished: false, 
     decodeStrings: false, 
     defaultEncoding: 'utf8', 
     length: 0, 
     writing: false, 
     sync: false, 
     bufferProcessing: false, 
     onwrite: [Function], 
     writecb: null, 
     writelen: 0, 
     buffer: [], 
     errorEmitted: false }, 
    writable: true, 
    allowHalfOpen: true, 
    onend: [Function], 
    destroyed: false, 
    bytesRead: 4987, 
    _bytesDispatched: 1231, 
    _pendingData: null, 
    _pendingEncoding: '', 
    server: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     _connections: 6, 
     connections: [Getter/Setter], 
     _handle: [Object], 
     _usingSlaves: false, 
     _slaves: [], 
     allowHalfOpen: true, 
     httpAllowHalfOpen: false, 
     timeout: 5000, 
     _connectionKey: '4:0.0.0.0:8080' }, 
    _idleTimeout: 120000, 
    _idleNext: 
     { _connecting: false, 
     _handle: [Object], 
     _readableState: [Object], 
     readable: true, 
     domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     _writableState: [Object], 
     writable: true, 
     allowHalfOpen: false, 
     onend: null, 
     destroyed: false, 
     bytesRead: 14384, 
     _bytesDispatched: 1798, 
     _pendingData: null, 
     _pendingEncoding: '', 
     _idleTimeout: 30000, 
     _idleNext: [Object], 
     _idlePrev: [Circular], 
     _idleStart: 1453475881306, 
     _monotonicStartTime: 898918969, 
     pipe: [Function], 
     addListener: [Function: addListener], 
     on: [Function: addListener], 
     pause: [Function], 
     resume: [Function], 
     read: [Function], 
     _consuming: true }, 
    _idlePrev: { _idleNext: [Circular], _idlePrev: [Object] }, 
    _idleStart: 1453475881568, 
    _monotonicStartTime: 898919231, 
    parser: 
     { _headers: [], 
     _url: '', 
     onHeaders: [Function: parserOnHeaders], 
     onHeadersComplete: [Function: parserOnHeadersComplete], 
     onBody: [Function: parserOnBody], 
     onMessageComplete: [Function: parserOnMessageComplete], 
     socket: [Circular], 
     incoming: [Circular], 
     maxHeaderPairs: 2000, 
     onIncoming: [Function] }, 
    ondata: [Function], 
    _paused: false, 
    _httpMessage: 
     { domain: null, 
     _events: [Object], 
     _maxListeners: 10, 
     output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: false, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     sendDate: true, 
     _headerSent: false, 
     _header: '', 
     _hasBody: true, 
     _trailer: '', 
     finished: false, 
     _hangupClose: false, 
     socket: [Circular], 
     connection: [Circular], 
     _headers: [Object], 
     _headerNames: [Object], 
     write: [Function], 
     end: [Function] } }, 
    _consuming: false, 
    _dumped: false, 
    httpVersionMajor: 1, 
    httpVersionMinor: 1, 
    upgrade: false, 
    originalUrl: '/api/ebay//*...omitted...*/', 
    _parsedUrl: 
    { protocol: null, 
    slashes: null, 
    auth: null, 
    host: null, 
    port: null, 
    hostname: null, 
    hash: null, 
    search: null, 
    query: null, 
    pathname: '/api/ebay//*...omitted...*/', 
    path: '/api/ebay//*...omitted...*/', 
    href: '/api/ebay//*...omitted...*/' }, 
    query: {}, 
    body: {} } 
+1

你需要提供你已经为这个和一些链接到您查阅过的特定易趣文档编写的代码,否则就更难有人来帮助你。谢谢! –

+0

我没想过要么。我的问题是eBay发送的回复中我可以找到XML。因为response.body是空的。 – Omgabee

+1

@omouse好的,我添加了所需的信息。如果有人打算低估这个问题,会提供一些关于他们为什么拒绝投票的信息,我会觉得这很有帮助。我只是想获得一些帮助。谢谢:) – Omgabee

回答

1

有名为每当响应有数据,该数据触发“数据”的响应事件。我们可以非常快速地获得响应状态码,但数据可能需要一些时间才能加载。

试试这个:

response.on('data', function handleResponse(data) { 
    console.log("data from the response"); 
    console.log(data); 
}); 
+0

我的意思是说在请求没有回应,我的错误。但我尝试添加该功能,并没有记录任何内容。我正在编辑我的问题与什么是记录(我正在记录请求)。我还添加了我正在尝试登录/访问数据的评论。 – Omgabee

+1

我最终得到了它的工作:)我确实使用了你的答案,但它花了我很长时间,所以很多试图让它记录我想要的。我重写了我的路线并在response.end()之后使用了它,并且它看起来有效。感谢您的帮助 – Omgabee

+0

真棒很高兴它为你工作! –