Node-SpliderApi icon indicating copy to clipboard operation
Node-SpliderApi copied to clipboard

httpServer.js function bug.

Open alex1504 opened this issue 6 years ago • 0 comments

function httpGet(host, data, path, status) {
  let options = {
    host: host,
    port: 80,
    // path missing '?' symbol make path different. 
    path: path + querystring.stringify(data),
    method: 'GET',
    encoding: null,
    headers: {
      'Content-Type': 'application/json',
      'User-Agent':
        'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'
    }
  }

  if (status) {
    http = require('https')
    options.port = 443
  }

  return new Promise(function(resolve, reject) {
    let body = ''
    let get_req = http.request(options, function(response) {

      response.on('data', function(chunk) {
        body += chunk
      })

      response.on('end', () => {
        resolve(body)
      })

      response.on('error', err => {
        reject(err)
      })
    })
    get_req.end()
  })
}
function httpPost(host, data, path, status) {
  // SyntaxError: Identifier 'data' has already been declared, remove let declaration.
  let data = querystring.stringify(data)
  let options = {
    host: host,
    port: '80',
    path: path,
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'User-Agent':
        'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36',
      'Content-Length': Buffer.byteLength(data) 
    }
  }

  if (status) {
    http = require('https')
    options.port = 443
  }
  return new Promise(function(resolve, reject) {
    let body = ''
    let post_req = http.request(options, function(response) {

      response.on('data', function(chunk) {
        body += chunk
      })

      response.on('end', () => {
        resolve(body)
      })

      response.on('error', err => {
        reject(err)
      })
    })

    post_req.write(data)
    post_req.end()
  })
}

alex1504 avatar Dec 10 '18 07:12 alex1504