溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

node作為中間服務(wù)層如何發(fā)送請求(發(fā)送請求的實(shí)現(xiàn)方法詳解)

發(fā)布時(shí)間:2020-09-11 17:26:52 來源:腳本之家 閱讀:152 作者:itKingOne 欄目:web開發(fā)

GET請求:

var http = require('http'); 
var qs = require('querystring'); 
var data = { 
  a: 123, 
  time: new Date().getTime()};//這是需要提交的數(shù)據(jù) 
var content = qs.stringify(data); 
var options = { 
  hostname: '127.0.0.1', 
  port: 10086, 
  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請求:

var http = require('http'); 
var qs = require('querystring'); 
var post_data = { 
  a: 123, 
  time: new Date().getTime()};//這是需要提交的數(shù)據(jù) 
var content = qs.stringify(post_data); 
var options = { 
  hostname: '127.0.0.1', 
  port: 10086, 
  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); 
  }); 
}); 
  
req.on('error', function (e) { 
  console.log('problem with request: ' + e.message); 
}); 
// write data to request body 
req.write(content); 
req.end();

以上這篇node作為中間服務(wù)層如何發(fā)送請求(發(fā)送請求的實(shí)現(xiàn)方法詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI