Home  >  Article  >  Web Front-end  >  Send requests using http module through nodejs (detailed tutorial)

Send requests using http module through nodejs (detailed tutorial)

亚连
亚连Original
2018-06-13 10:13:145813browse

This article mainly introduces the method of nodejs using the http module to send get and post requests. It analyzes the specific operation techniques of nodejs based on the http module to send get and post requests. Friends in need can refer to it

The example in this article describes how nodejs uses the http module to send get and post requests. Share it with everyone for your reference, the details are as follows:

GET request

var http = require('http');
var querystring = require('querystring');
var data = {
  a: 123,
  time: new Date().getTime()};//这是需要提交的数据
var content = querystring.stringify(data);
var options = {
  hostname: '127.0.0.1',
  port: 3000,
  path: '/pay/pay_callback?' + content,
  method: 'GET'
};
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);
});
req.end();

POST request

var http = require('http');
var querystring = require('querystring');
var post_data = {
  a: 123,
  time: new Date().getTime()};//这是需要提交的数据
var content = querystring.stringify(post_data);
var options = {
  hostname: '127.0.0.1',
  port: 3000,
  path: '/pay/pay_callback',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  }
};
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);
  //JSON.parse(chunk)
  });
});
req.on('error', function (e) {
  console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(content);
req.end();

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed answers to the construction of Webpack Babel React environment (detailed tutorial)

Detailed explanation of the relevant configuration of webpack babel (detailed Tutorial)

Using webpack vue2 for project construction

Reporting a 404 problem regarding the vue project resource file in webpack (detailed tutorial)

How to integrate vux in vue.js to implement pull-up loading and pull-down refresh

The above is the detailed content of Send requests using http module through nodejs (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn