美文网首页
工具:requests

工具:requests

作者: 一个有想法的人 | 来源:发表于2018-03-24 21:00 被阅读0次

    安装

    npm install request --save
    

    发出get请求

    request('http://httpbin.org/ip', (err, resp, body) => {
            console.log(body)
        })
    

    通用请求

      var request = require('request')
      request(
        { method: 'GET'
        , uri: 'http://www.google.com'
        , gzip: true
        }
      , function (error, response, body) {
          // body is the decompressed response body
          console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
          console.log('the decoded data is: ' + body)
        }
      )
      .on('data', function(data) {
        // decompressed data as it is received
        console.log('decoded chunk: ' + data)
      })
      .on('response', function(response) {
        // unmodified http.IncomingMessage object
        response.on('data', function(data) {
          // compressed data as it is received
          console.log('received ' + data.length + ' bytes of compressed data')
        })
      })
    

    相关文章

      网友评论

          本文标题:工具:requests

          本文链接:https://www.haomeiwen.com/subject/dtdccftx.html