File tree Expand file tree Collapse file tree 1 file changed +39
-1
lines changed Expand file tree Collapse file tree 1 file changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -201,7 +201,45 @@ node app.js
201
201
202
202
## Q. *** How to make an HTTP POST request using Node.js?***
203
203
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
+ ```
205
243
206
244
<div align =" right " >
207
245
<b><a href="#">↥ back to top</a></b>
You can’t perform that action at this time.
0 commit comments