This article mainly introduces Node. in js, the Request module provides a basic tutorial on how to process HTTP requests. The request also supports OAuth signature requests, which are very powerful. If you need them, refer to the following section to introduce a Node. js module -- request. With this module, http requests become super simple.
The Request is simple 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 }})
Stream:
Any response can be output to the file stream.
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
In turn, you can also send the file to a PUT or POST request. If no header is provided, the file suffix 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 '))
You can also send requests to yourself through pipe. In this case, the original content-type and content-length will be retained.
Request. get ('HTTP: // google.com/img.png'). pipe (request. put ('HTTP: // mysite.com/img.png '))
Form:
Request supports form upload using application/x-www-form-urlencoded and multipart/form-data.
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'})
When using multipart/form-data, you don't have to worry about setting headers or other things. The request will help you solve them.
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(path.join(__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, which is true by default, sends a basic authentication header. If it is set to false, the system will try again upon receiving the 401 request (the server's 401 response must contain the WWW-Authenticate authentication method ).
When sendImmediately is true, Digest authentication is supported.
OAuth Logon:
// Twitter OAuthvar qs = require('querystring') , oauth = { callback: 'http://mysite.com/callback/' , consumer_key: CONSUMER_KEY , consumer_secret: CONSUMER_SECRET} , url = 'https://api.twitter.com/oauth/request_token';request.post({url:url, oauth:oauth}, function (e, r, body) { // Ideally, you would take the body in the response // and construct a URL that a user clicks on (like a sign in button). // The verifier is only available in the response after a user has // verified with twitter that they are authorizing your app. var access_token = qs.parse(body) , oauth = { consumer_key: CONSUMER_KEY , consumer_secret: CONSUMER_SECRET , token: access_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 the favorites and derived numbers of a repository. We use the 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 == 200) { var info = JSON.parse(body); console.log(info.stargazers_count +"Stars"); console.log(info.forks_count +"Forks");}}request(options, callback);
Cookies:
Cookies are disabled by default. Set jar to true in ults or options so that all 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 a global cookie jar.
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: setCookie requires at least three parameters, and the last one is the callback function.
You can use the pipe method of request to conveniently obtain the file stream of the image.
var request = require('request'), fs = require('fs'); request('https://www.google.com.hk/images/srpr/logo3w.png').pipe(fs.createWriteStream('doodle.png'));
For more usage and instructions, click here to continue reading: https://github.com/mikeal/request/
Example
A simple example is provided here to capture the data of the hotel query on the Qunar Network (obtain the price ranking of each room type in a certain period of time ):
Var request = require ('request'), fs = require ('fs'); var reqUrl = 'HTTP: // developer.qunar.com/price/detail.jsp? FromDate = 2012-08-18 & toDate = 2012-08-19 & cityurl = shanghai_city & region seq = shanghai_city_2856 & cn = 5'; request ({uri: reqUrl}, function (err, response, body) {// console. log (response. statusCode); // console. log (response); // if the data volume is large, you need to store the returned data based on the date and hotel ID, if the file var filePath = _ dirname + '/data is directly read when the obtained data is compared. 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 is derived from a friend who is a hotel business and wants to know the competitiveness of the price that he provides to the customer on the website:
1. If the price provided is too low, the amount of money you earn will be less. Therefore, if your price is the lowest, you need to check the second low and decide whether to adjust it;
2. If the price provided is too high, the search results will be relatively low. No customer can book a hotel and the business will be lost.
Because there are a lot of hotel booking businesses, such as more than two thousand, it is relatively passive to rely on human queries one by one, and it is difficult to expand, therefore, it is feasible for me to analyze this demand and make a good real-time warning system (of course, data 5 ~ The page is automatically refreshed once every 10 minutes ). In this way, the maximum profit can be guaranteed, the efficiency of sales and customer departments can be improved, and the number of hotel cooperation and the company's staff expansion can be accelerated:
1. Do not lose money or buy or sell at a loss;
2. If the provided price is found to be too low or too high, you must call the platform's api to directly modify the price;
3. The automatic generation of analysis reports function is provided to analyze changes in the price adjustment policies of competitors;