@@ -2640,10 +2640,54 @@ Content-Security-Policy: default-src 'self' *.http://sometrustedwebsite.com
2640
2640
<b><a href="#table-of-contents">↥ back to top</a></b>
2641
2641
</div >
2642
2642
2643
- ## Q. How to make an HTTP POST request using Node.js?
2643
+ ## Q. How to make an HTTP POST request using axios in Node.js?
2644
2644
2645
2645
``` js
2646
- // ToDo
2646
+ /**
2647
+ * POST Request using Axios
2648
+ */
2649
+ const express = require (" express" );
2650
+ const app = express ();
2651
+ const axios = require (" axios" );
2652
+
2653
+ app .post (" /user" , async (req , res ) => {
2654
+ try {
2655
+ const payload = { name: ' Aashita Iyer' , email: ' aashita.iyer@email.com' };
2656
+
2657
+ const response = await axios .post (' http://httpbin.org/post' , payload);
2658
+ console .log (response .data );
2659
+ res .status (200 ).json (response .data );
2660
+ } catch (err) {
2661
+ res .status (500 ).json ({ message: err });
2662
+ }
2663
+ });
2664
+
2665
+ app .listen (3000 , function () {
2666
+ console .log (` App listening at http://localhost:3000/` );
2667
+ });
2668
+ ```
2669
+
2670
+ ** Output:**
2671
+
2672
+ ``` js
2673
+ {
2674
+ args: {},
2675
+ data: ' {"name":"Aashita Iyer","email":"aashita.iyer@email.com"}' ,
2676
+ files: {},
2677
+ form: {},
2678
+ headers: {
2679
+ Accept: ' application/json, text/plain, */*' ,
2680
+ ' Accept-Encoding' : ' gzip, deflate, br' ,
2681
+ ' Content-Length' : ' 56' ,
2682
+ ' Content-Type' : ' application/json' ,
2683
+ Host: ' httpbin.org' ,
2684
+ ' User-Agent' : ' axios/1.1.3' ,
2685
+ ' X-Amzn-Trace-Id' : ' Root=1-635cd3d3-1f13ea981467e6371ce3a740'
2686
+ },
2687
+ json: { email: ' aashita.iyer@email.com' , name: ' Aashita Iyer' },
2688
+ origin: ' xx.xx.xx.xx' ,
2689
+ url: ' http://httpbin.org/post'
2690
+ }
2647
2691
```
2648
2692
2649
2693
<div align =" right " >
0 commit comments