2016-01-20 61 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

真棒很高興它爲你工作! –