Skip to content

Commit bfec48d

Browse files
authored
Update README.md
1 parent a760378 commit bfec48d

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,45 @@ node app.js
201201

202202
## Q. ***How to make an HTTP POST request using Node.js?***
203203

204-
*ToDo*
204+
```js
205+
const https = require('https')
206+
207+
208+
const obj = {
209+
"userId":1,
210+
"id":1,
211+
"title":"whatever",
212+
"completed":false
213+
}
214+
215+
const data = JSON.stringify(obj)
216+
217+
const options = {
218+
hostname: 'jsonplaceholder.typicode.com',
219+
port: 443,
220+
path: '/todos',
221+
method: 'POST',
222+
headers: {
223+
'Content-Type': 'application/json',
224+
'Content-Length': data.length
225+
}
226+
}
227+
228+
const req = https.request(options, res => {
229+
console.log(`statusCode: ${res.statusCode}`)
230+
231+
res.on('data', d => {
232+
process.stdout.write(d)
233+
})
234+
})
235+
236+
req.on('error', error => {
237+
console.error(error)
238+
})
239+
240+
req.write(data)
241+
req.end()
242+
```
205243

206244
<div align="right">
207245
<b><a href="#">↥ back to top</a></b>

0 commit comments

Comments
 (0)