Node.js the Basic tutorial for processing HTTP protocol requests in the request module _node.js

Source: Internet
Author: User
Tags auth http authentication oauth require setcookie

Here to introduce a node.js module--request. With this module, HTTP requests become very simple.

The request is simple to use, and supports both HTTPS and redirection.

var request = require (' request ');
Request (' http://www.google.com ', function (error, response, body) {
 if (!error && response.statuscode = 200 ) {
 Console.log (body)//Print Google homepage
}
})

Flow:

Any response can be output to a file stream.

Request (' http://google.com/doodle.png '). Pipe (Fs.createwritestream (' doodle.png '))

Conversely, you can also pass a file to a put or POST request. If no header is supplied, the file suffix name is detected and the corresponding Content-type is set in the put request.

Fs.createreadstream (' File.json '). Pipe (Request.put (' Http://mysite.com/obj.json '))
Requests can also be pipe to themselves. In this case, the original Content-type and content-length are preserved.

Request.get (' http://google.com/img.png '). Pipe (Request.put (' http://mysite.com/img.png '))
form:

Request supports application/x-www-form-urlencoded and multipart/form-data to implement form uploads.

X-www-form-urlencoded is simple:

Request.post (' Http://service.com/upload ', {form:{key: ' Value '}})

Or:

Request.post (' Http://service.com/upload '). Form ({key: ' Value '})

Use Multipart/form-data don't worry about setting headers and the like, and the request will help you out.

var r = request.post (' http://service.com/upload ')
var form = R.form ()
form.append (' My_field ', ' My_value ')
form.append (' My_buffer ', new buffer ([1, 2, 3]))
form.append (' My_file ', Fs.createreadstream (__ DirName, ' doodle.png ')
form.append (' Remote_file ', request (' Http://google.com/doodle.png '))

HTTP Authentication:

Request.get (' http://some.server.com/'). Auth (' username ', ' password ', false);

Or

Request.get (' http://some.server.com/', {
 ' auth ': {'
 user ': ' username ', ' pass
 ': ' Password ',
 ' Sendimmediately ': False
}
});

Sendimmediately, the default is true, send a Basic authentication header. When set to False, 401 will be retried (the server's 401 response must contain the Www-authenticate specified authentication method).

Support Digest authentication when sendimmediately is true.

OAuth Login:

Twitter OAuth var qs = require (' querystring '), OAuth = {callback: ' http://mysite.com/callback/', consumer_key:c
Onsumer_key, consumer_secret:consumer_secret}, url = ' Https://api.twitter.com/oauth/request_token '; Request.post ({url:url, Oauth:oauth}, Function (E, R, body) {//Ideally, U would take the body in the response//and
 Construct a URL that's a user clicks on (like a sign in button). The verifier is only available on the response after a user has//verified with Twitter This they are authorizing you
 R app. var access_token = Qs.parse (body), OAuth = {consumer_key:consumer_key, Consumer_secret:consumer_secret, token:a
Ccess_token.oauth_token, verifier:access_token.oauth_verifier}, url = ' Https://api.twitter.com/oauth/access_token '
 ; Request.post ({url:url, Oauth:oauth}, Function (E, R, body) {var perm_token = Qs.parse (body), OAuth = {consumer_key: Consumer_key, Consumer_secret:consumer_secret, Token:perm_token.oauth_token, toKen_secret:perm_token.oauth_token_secret}, url = ' Https://api.twitter.com/1/users/show.json? '
 , params = {screen_name:perm_token.screen_name, user_id:perm_token.user_id}; URL + qs.stringify (params) request.get ({url:url, Oauth:oauth, json:true}, Function (e, r, user) {Console.log (user)})}
 )
})

Custom HTTP Header

User-agent can be set in the Options object. In the following example, we call the GitHub API to find out the number of collections and derivations of a warehouse. We use custom user-agent and HTTPS.

var request = require (' request ');

var options = {
 URL: ' https://api.github.com/repos/mikeal/request ',
 headers: {
 ' user-agent ': ' Request '
}
};

function callback (Error, response, body) {
 if (!error && response.statuscode =) {
 var info = Json.par SE (body);
 Console.log (Info.stargazers_count + "stars");
 Console.log (Info.forks_count + "forks");
}

Request (options, callback);

Cookies

Cookies are disabled by default. Set the jar to true in defaults or options so that subsequent requests use cookies.

var request = Request.defaults ({jar:true})
request (' http://www.google.com ', function () {
request (' http:// Images.google.com ')
})

By creating a new instance of Request.jar (), you can use a custom cookie instead of the cookie jar Global.

var j = Request.jar ()
var request = Request.defaults ({jar:j})
request (' http://www.google.com ', function () {
request (' http://images.google.com ')
})

Or

var j = Request.jar ()
var cookie = Request.cookie (' your_cookie_here ')
j.setcookie (cookie, URI, function (err, Cookie)
request ({URL: ' http://www.google.com ', jar:j}, function () {
request (' http://images.google.com ') )
})

Note that the Setcookie requires at least three parameters, and the last one is the callback function.

You can use the pipe method of request to get a picture file stream conveniently

 var request = require (' request '),
 fs = require (' FS ');
 
 Request (' https://www.google.com.hk/images/srpr/logo3w.png '). Pipe (Fs.createwritestream (' doodle.png '));

More methods and instructions can be clicked here to continue reading: https://github.com/mikeal/request/

Example

Here is a very simple example, to crawl to where the network of Hotel query data (get the hotel for a certain period of time the price ranking of each room):

 var request = require (' request '),
 fs = require (' FS ');
 
 
 var requrl = ' Http://hotel.qunar.com/price/detail.jsp?fromDate=2012-08-18&toDate=2012-08-19&cityurl= Shanghai_city&hotelseq=shanghai_city_2856&cn=5 ';
 
 Request ({Uri:requrl}, function (err, response, body) {
 
 //console.log (response.statuscode);
 Console.log (response);
 
 If the amount of data is large, you need to return the data based on the date, the hotel ID to store, if the data to compare the direct read file
 var filePath = __dirname + '/data/data.js ';
 
 if (fs.exists (FilePath)) {
  fs.unlinksync (filePath);
 
  Console.log (' Del file ' + FilePath);
 }
 
 Fs.writefile (FilePath, body, ' UTF8 ', function (err) {
  if (err) {
  throw err;
  }
 
  Console.log (' Save ' + filePath + ' ok~ ');
 });
 
 Console.log (' Fetch ' + requrl + ' ok~ ');
 });

This example comes from a hotel business friend who wants to know how competitive the price is for a customer on the website:

1, if the price is too low, the money they earn will be less, so if their price is the lowest, you need to see the second low is how much, and then decide whether to adjust;

2, if the price is too high, then the results of the search out of the rankings are more lean, no customers to book hotels, business is not

Because of the hotel booking business many, such as more than 2000, if one relies on the human to query ranking is relatively passive, and it is difficult to bigger, so his demand I analyzed it is feasible, and can be made a good real-time warning system (of course, the data will be automatically refreshed in 5-10 minutes). In this way to ensure maximum profit, improve sales, customer department efficiency, speed up the number of hotel cooperation and the company's personnel expansion:

1, does not lose money, loses the sale does not do;

2, if found to provide the price is too low or too high, need to support the API interface to call the platform, directly modify the price;

3, has the automatic Generation Analysis report function, analyzes the competitor in the price adjustment strategy change situation;

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.