{"id":12104,"date":"2025-04-16T11:51:35","date_gmt":"2025-04-16T11:51:35","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=12104"},"modified":"2025-04-16T11:51:35","modified_gmt":"2025-04-16T11:51:35","slug":"make-http-post-request-in-node-js","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/node\/make-http-post-request-in-node-js","title":{"rendered":"How is an HTTP POST request made in node.js?"},"content":{"rendered":"<p>To make an outbound HTTP POST request with data in Node.js, you can use the built-in http or https modules or a library like axios or node-fetch for simplicity.<\/p>\n<h3>1. Using the https mode<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nconst https = require('https');\r\nconst data = JSON.stringify({\r\n    title: 'foo',\r\n    body: 'bar',\r\n    userId: 1,\r\n});\r\n\r\nconst options = {\r\n    hostname: 'jsonplaceholder.typicode.com',\r\n    path: '\/posts',\r\n    method: 'POST',\r\n    headers: {\r\n        'Content-Type': 'application\/json',\r\n        'Content-Length': data.length,\r\n    },\r\n};\r\nconst req = https.request(options, (res) =&gt; {\r\n    let responseBody = '';\r\n    res.on('data', (chunk) =&gt; {\r\n        responseBody += chunk;\r\n    });\r\n    res.on('end', () =&gt; {\r\n        console.log('Response:', JSON.parse(responseBody));\r\n    });\r\n});\r\nreq.on('error', (error) =&gt; {\r\n    console.error(`Error: ${error.message}`);\r\n});\r\n\r\n\/\/ Write data to the request body\r\nreq.write(data);\r\nreq.end();\r\n<\/pre>\n<h3>2. Using axios<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nconst axios = require('axios');\r\n\r\nconst data = {\r\n    title: 'foo',\r\n    body: 'bar',\r\n    userId: 1,\r\n};\r\n\r\naxios.post('https:\/\/jsonplaceholder.typicode.com\/posts', data)\r\n    .then((response) => {\r\n        console.log('Response:', response.data);\r\n    })\r\n    .catch((error) => {\r\n        console.error('Error:', error.message);\r\n    });\r\n<\/pre>\n<h3>3. Using node-fetch<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nconst fetch = require('node-fetch');\r\nconst data = {\r\n    title: 'foo',\r\n    body: 'bar',\r\n    userId: 1,\r\n};\r\nfetch('https:\/\/jsonplaceholder.typicode.com\/posts', {\r\n    method: 'POST',\r\n    headers: {\r\n        'Content-Type': 'application\/json',\r\n    },\r\n    body: JSON.stringify(data),\r\n})\r\n    .then((response) => response.json())\r\n    .then((data) => {\r\n        console.log('Response:', data);\r\n    })\r\n    .catch((error) => {\r\n        console.error('Error:', error.message);\r\n    });\r\n<\/pre>\n<h3>4. Using the request Library<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nconst request = require('request');\r\nconst data = {\r\n    title: 'foo',\r\n    body: 'bar',\r\n    userId: 1,\r\n};\r\nconst options = {\r\n    url: 'https:\/\/jsonplaceholder.typicode.com\/posts',\r\n    method: 'POST',\r\n    json: true,\r\n    body: data,\r\n};\r\nrequest(options, (error, response, body) => {\r\n    if (error) {\r\n        console.error('Error:', error);\r\n    } else {\r\n        console.log('Response:', body);\r\n    }\r\n});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To make an outbound HTTP POST request with data in Node.js, you can use the built-in http or https modules or a library like axios or node-fetch for simplicity. 1. Using the https mode const https = require(&#8216;https&#8217;); const data = JSON.stringify({ title: &#8216;foo&#8217;, body: &#8216;bar&#8217;, userId: 1, }); const options = { hostname: &#8216;jsonplaceholder.typicode.com&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12105,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-12104","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12104"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=12104"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12104\/revisions"}],"predecessor-version":[{"id":12107,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12104\/revisions\/12107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/12105"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=12104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=12104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=12104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}