To create an HTTP POST request using node.js, you can use the built-in http
or https
module. Here is an example code snippet:
const https = require('https');
const data = JSON.stringify({
message: 'Hello World!',
});
const options = {
hostname: 'example.com',
port: 443,
path: '/api/messages',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
},
};
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (error) => {
console.error(error);
});
req.write(data);
req.end();
In this example, we are making an HTTPS POST request to example.com/api/messages
with a JSON payload of { message: 'Hello World!' }
. The request headers include the Content-Type
and Content-Length
fields. The response from the server is logged to the console.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-02 18:25:30 +0000
Seen: 19 times
Last updated: May 02 '23
What is the method for altering the color of the input text in a TextFormField in Flutter?
What is the procedure for utilizing the node-rdpjs library?
What is the method to change a PDF file to an Excel file using C#?
What is the method to make a TextButton inactive when the text field has no text?
What is the method to alter the background image in HTML?
What is the method for saving an entity with @EmbeddedId as its primary key in Hibernate?
What is the method to superimpose two seaborn density plots?