The http. request Method in node. js is described in node. jsrequest.

Source: Internet
Author: User
Tags unix domain socket

The http. request Method in node. js is described in node. jsrequest.

Method description:

The function room acts as a client to initiate a request to the HTTP server.

Syntax:

Copy codeThe Code is as follows:
Http. get (options, callback)

Because this method belongs to the http module, the http module (var http = require ("http") must be introduced before use "))

Receiving parameters:

Option array object, which includes the following parameters:

Host: The domain name or IP address of the requested website ). The default value is 'localhost '.

Hostname: the server name. The host name is the preferred value.

Port: port of the requesting website. The default value is 80.

LocalAddress: The local location where the network connection is established

SocketPath: Unix Domain Socket (Domain Socket path)

Method: Specifies the HTTP request method. The default value is 'get '.

Path: the path of the Request relative to the root. The default value is '/'. QueryString should be included. For example:/index.html? Page = 12

Headers: request header object.

Auth: Basic Authentication (Basic Authentication). This value is calculated as the Authorization part in the request header.

Callback: callback. A parameter is passed to the http. ClientResponse instance. Http. request returns an http. ClientRequest instance.

Example:

Copy codeThe Code is as follows:
Var options = {
Hostname: 'www .google.com ',
Port: 80,
Path: '/upload ',
Method: 'post'
};
 
Var req = http. request (options, function (res ){
Console. log ('status: '+ res. statusCode );
Console. log ('headers: '+ JSON. stringify (res. HEADERS ));
Res. setEncoding ('utf8 ');
Res. on ('data', function (chunk ){
Console. log ('body: '+ chunk );
});
});
 
Req. on ('error', function (e ){
Console. log ('problem with request: '+ e. message );
});
 
// Write data to request body
Req. write ('data \ n ');
Req. write ('data \ n ');
Req. end ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.