2017-08-30 60 views
1

我試圖向接受JSON的端點發送POST請求,但它不起作用。我是否必須發送任何特定參數才能讓網絡知道它被編碼爲JSON?如何使用NodeJS在POST請求中發送JSON

下面是簡單的要求我到目前爲止:

var request = require('request') 

var cookie = '**Here the cookie copied from the Network tab from the Chrome Dev Tools Bar**' 
var UA = '**Here the UA copied from the Network tab from the Chrome Dev Tools Bar**' 

var JSONformData = {"jsonrpc":"2.0","method":"LMT_split_into_sentences","params":{"texts":["Text"],"lang":{"lang_user_selected":"auto","user_preferred_langs":["EN","ES"]}},"id":8} 

var URL = 'https://www.deepl.com/jsonrpc' 

request.cookie(cookie) 
request.post({ 
     url: URL, 
     headers: { 
      'User-Agent': UA 
     }, 
     form: JSONformData 
    }, function(error, response, body) { 
     console.log(response) 
    } 
) 

回答

3

如果您發送的JSON數據那麼你並不需要指定的形式,而不是指定的選項數據JSON對象:

request.post({ 
     url: URL, 
     headers: { 
      'User-Agent': UA 
     }, 
     json: JSONformData 
    }, function(error, response, body) { 
     console.log(response) 
    }) 
+0

您必須在代碼中做錯了什麼。在這裏查看文檔:https://github.com/request/request#requestoptions-callback使用json鍵指定Json數據將設置json和body的內容類型爲json數據。 –

+0

正如你所看到的: https://github.com/request/request#forms body:JSON.stringify(...) 這就是關鍵點 –

+0

這是什麼意思?他想用請求模塊發送Json數據,這就是文檔如何指定它的完成 –

相關問題