我正在学习node,但不知道如何用字符串发送POST请求,这是我的代码。
const h = require('https');
h.post("https://VLang.johnstev111.repl.co", "print('test')")
我把内容类型设置为'textplain',你可以通过设置postData变量来改变数据。
const https = require('https');
// Put your post data here
const postData = "print(40 + 2)";
const options = {
hostname: 'VLang.johnstev111.repl.co',
method: 'POST',
headers: {
'Content-Length': postData.length
}
};
const req = https.request(options, (res) => {
console.log('Response: status:', res.statusCode);
console.log('Response: headers:', res.headers);
res.on('data', (d) => {
process.stdout.write("Response: " + d);
});
});
req.on('error', (e) => {
console.error("An error occurred:", e);
});
req.write(postData);
req.end();