Skip to content

Commit 150a012

Browse files
committed
Update README.md
1 parent af2c86b commit 150a012

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

README.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,10 +2640,54 @@ Content-Security-Policy: default-src 'self' *.http://sometrustedwebsite.com
26402640
<b><a href="#table-of-contents">↥ back to top</a></b>
26412641
</div>
26422642

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?
26442644

26452645
```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+
}
26472691
```
26482692

26492693
<div align="right">

0 commit comments

Comments
 (0)