我正在尝试向接受 JSON 的端点发送 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)
}
)
最佳答案
如果您要发送 JSON 数据,则无需指定表单,而是在选项对象中为数据指定 json:
request.post({
url: URL,
headers: {
'User-Agent': UA
},
json: JSONformData
}, function(error, response, body) {
console.log(response)
})
关于json - 如何使用 NodeJS 在 POST 请求中发送 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45955865/