如何在Node.js中用字符串发送post请求?

问题描述 投票:0回答:1

我正在学习node,但不知道如何用字符串发送POST请求,这是我的代码。

const h = require('https');
h.post("https://VLang.johnstev111.repl.co", "print('test')")
node.js http post
1个回答
0
投票

我把内容类型设置为'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();
© www.soinside.com 2019 - 2024. All rights reserved.