Node.js http.ClientRequest.end() API
Last Updated :
08 Jul, 2021
The http.ClientRequest.end() is an inbuilt application programming interface of class ClientRequest within http module which is used to finish sending the request. If any parts of the body are unsent.
Syntax:
const request.end([data[, encoding]][, callback])
Parameters: This method takes the data as an input.
Return Value : This method returns nothing but a callback function.
Example 1:
Javascript
const http = require( 'http' );
http.createServer((req, res) => {}).listen(3000, '127.0.0.1' , () => {
const req = http.request({
port: 3000,
host: '127.0.0.1' ,
});
req.setHeader( 'content-type' , 'text/html' );
req.end()
console.log( "header :- " + req.getHeader( 'content-type' ))
process.exit(0)
});
|
Output:
Example 2:
Javascript
const http = require( 'http' );
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type' : 'text/plain' });
res.end( 'okay' );
});
server.listen(3000, '127.0.0.1' , () => {
const options = {
port: 3000,
host: '127.0.0.1' ,
headers: {
'Connection' : 'Upgrade' ,
'Upgrade' : 'websocket'
}
};
const req = http.request(options);
req.setHeader( 'Cookie' , [ 'type=ninja' , 'language=javascript' ]);
const v = req.getHeader( 'Cookie' );
req.end()
console.log( "header :- " + v)
process.exit(0)
});
|
Output:
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_request_end_data_encoding_callback
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...