"property is missing and it is not optional" 尝试在 post 请求中写入数据后
"property is missing and it is not optional" after trying to write data in post request
我正在尝试将正文数据添加到 Electron.js 中的 post 请求中。但是数据写入了一些其他的属性。如何在请求中正确写入数据?
function tokenRequest() {
var postData = JSON.stringify({
username: USERNAME,
password: PASSWORD,
});
const tokenRequest = net.request({
method: 'POST',
protocol: 'https:',
hostname: HOST,
port: PORT,
path: '/api2/json/access/ticket',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
});
tokenRequest.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
response.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
response.on('end', () => {
console.log('No more data in tokenRequest response.');
});
});
tokenRequest.write(postData);
tokenRequest.end();
}
控制台输出:
因为 Proxmox 只接受 URL 编码的 JSON 数据 (source) you have to replace your JSON.stringify()
with new URLSearchParams()
。在写入之前还要将 toString()
添加到 postData
。这里是完整代码:
function tokenRequest() {
const postData = new URLSearchParams({ // <----
username: USERNAME,
password: PASSWORD,
})
const tokenRequest = net.request({
method: 'POST',
protocol: 'https:',
hostname: HOST,
port: PORT,
path: '/api2/json/access/ticket',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
})
tokenRequest.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`)
console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
response.on('data', (chunk) => console.log(`BODY: ${chunk}`))
response.on('end', () => console.log('No more data in tokenRequest response.'))
})
tokenRequest.write(postData.toString()) // <----
tokenRequest.end()
}
我正在尝试将正文数据添加到 Electron.js 中的 post 请求中。但是数据写入了一些其他的属性。如何在请求中正确写入数据?
function tokenRequest() {
var postData = JSON.stringify({
username: USERNAME,
password: PASSWORD,
});
const tokenRequest = net.request({
method: 'POST',
protocol: 'https:',
hostname: HOST,
port: PORT,
path: '/api2/json/access/ticket',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
});
tokenRequest.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
response.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
response.on('end', () => {
console.log('No more data in tokenRequest response.');
});
});
tokenRequest.write(postData);
tokenRequest.end();
}
控制台输出:
因为 Proxmox 只接受 URL 编码的 JSON 数据 (source) you have to replace your JSON.stringify()
with new URLSearchParams()
。在写入之前还要将 toString()
添加到 postData
。这里是完整代码:
function tokenRequest() {
const postData = new URLSearchParams({ // <----
username: USERNAME,
password: PASSWORD,
})
const tokenRequest = net.request({
method: 'POST',
protocol: 'https:',
hostname: HOST,
port: PORT,
path: '/api2/json/access/ticket',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
})
tokenRequest.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`)
console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
response.on('data', (chunk) => console.log(`BODY: ${chunk}`))
response.on('end', () => console.log('No more data in tokenRequest response.'))
})
tokenRequest.write(postData.toString()) // <----
tokenRequest.end()
}