如何使用NodeJS在POST请求中发送JSON
问题描述:
我试图向接受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)
}
)
答
如果您发送的JSON数据那么你并不需要指定的形式,而不是指定的选项数据JSON对象:
request.post({
url: URL,
headers: {
'User-Agent': UA
},
json: JSONformData
}, function(error, response, body) {
console.log(response)
})
您必须在代码中做错了什么。在这里查看文档:https://github.com/request/request#requestoptions-callback使用json键指定Json数据将设置json和body的内容类型为json数据。 –
正如你所看到的: https://github.com/request/request#forms body:JSON.stringify(...) 这就是关键点 –
这是什么意思?他想用请求模块发送Json数据,这就是文档如何指定它的完成 –