0

I am learning node, but can't figure out how to send a POST request with a string. Here is my code:

const h = require('https');
h.post("https://VLang.johnstev111.repl.co", "print('test')")
  • Does this answer your question? [How is an HTTP POST request made in node.js?](https://stackoverflow.com/questions/6158933/how-is-an-http-post-request-made-in-node-js) – O. Jones Jun 10 '20 at 11:17

1 Answers1

0

This should do pretty much what you wish, we log the response and any errors. I've set the content-type to 'text/plain', and you can change the data by setting the postData variable to whatever you wish.

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();
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40