2017-04-07 59 views
0

我試圖使用request POST FastSpring的API來更新我的客戶訂閱。我能夠毫無問題地獲得訂閱,但是當涉及到POST時,我似乎無法使其工作。使用請求HTTP客戶端庫發佈到FastSpring API

這裏是我的代碼:

 var options = { 
 
      method: 'POST', 
 
      url: 'https://api.fastspring.com/subscriptions', 
 
      headers: { 
 
       'Authorization': auth, 
 
       'User-Agent': 'request', 
 
       'Content-Type': 'application/json' 
 
      }, 
 
      formData : { 
 
       'subscriptions': [ 
 
        { 
 
         "subscription": subscriptionId, 
 
         "quantity": newQty, 
 
         "product": product, 
 
         "prorate": true 
 
        } 
 
       ] 
 
      } 
 
      
 
     }; 
 
      
 
     request.post(options, function (error, response, body) { 
 
      _logger.error('Fastspring API response: ' + JSON.stringify(response)); 
 
      _logger.error('Fastspring API error: ' + JSON.stringify(error)); 
 
      _logger.error('Fastspring API body: ' + JSON.stringify(body)); 
 
      if (!error && response.statusCode === 200) { 
 
       res.status(200).send(JSON.parse(body)); 
 
      } else { 
 
       _logger.error('Fastspring API Error: ' + error); 
 
       res.status(500).send({ 'error': error}); 
 
      } 
 
     });

試圖在下面的錯誤的POST結果的結果:

TypeError: source.on is not a function 

我看着它,看到的原因爲什麼它失敗是請求無法接受我發送的formData。問題是,如果沒有它,我不能發佈到FastSpring的API(如果我發送其他內容,我會收到錯誤消息「找不到訂閱」)。

我的問題是這樣的:我可以以任何方式發送我的formData,它將被請求接受嗎?我在請求的問題頁面上查找了此問題,無法找到滿意的答案。

回答

1

你想傳遞一些JSON數據嗎?通常這種數據不是寫在formData中的。

嘗試這樣:

var myreq = request.post(...) 
myreq.write(myJsonData); 
myreq.end(); 
+0

這工作!謝謝。對於將來看到這一點的人來說,JSON數據需要被串化爲正確發送(在這種情況下,JSON.stringify(myJsonData))。謝謝,@ mligor! – Juan

相關問題