久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      node怎么發(fā)出https請求

      方法:1、用HTTP模塊的“https.get()”方法發(fā)出get請求;2、用通用的“https.request()”方法發(fā)出post請求;3、用PUT和DELETE請求,只需將“options.method”改為PUT或DELETE即可。

      node怎么發(fā)出https請求

      本教程操作環(huán)境:windows10系統(tǒng)、nodejs 12.19.0版本、Dell G3電腦。

      node怎么發(fā)出https請求

      了解Node.js本機HTTPS模塊,該模塊可以在沒有任何外部依賴的情況下發(fā)出HTTP請求。

      由于它是本機模塊,因此不需要安裝。 您可以通過以下代碼訪問它:

      const https = require('https');

      GET請求

      是一個非常簡單的示例,該示例使用HTTP模塊的https.get()方法發(fā)送GET請求:

      const https = require('https'); https.get('https://reqres.in/api/users', (res) => {     let data = '';     // called when a data chunk is received.     res.on('data', (chunk) => {         data += chunk;     });     // called when the complete response is received.     res.on('end', () => {         console.log(JSON.parse(data));     }); }).on("error", (err) => {     console.log("Error: ", err.message); });

      與其他流行的HTTP客戶端收集響應并將其作為字符串或JSON對象返回的方法不同,在這里,您需要將傳入的數(shù)據(jù)流連接起來以供以后使用。 另一個值得注意的例外是HTTPS模塊不支持promise,這是合理的,因為它是一個低級模塊并且不是非常用戶友好。

      POST請求

      要發(fā)出POST請求,我們必須使用通用的https.request()方法。 沒有可用的速記https.post()方法。

      https.request()方法接受兩個參數(shù):

      • options —它可以是對象文字,字符串或URL對象。

      • callback —回調函數(shù),用于捕獲和處理響應。

      讓我們發(fā)出POST請求:

      const https = require('https'); const data = JSON.stringify({     name: 'John Doe',     job: 'DevOps Specialist' }); const options = {     protocol: 'https:',     hostname: 'reqres.in',     port: 443,     path: '/api/users',     method: 'POST',     headers: {         'Content-Type': 'application/json',         'Content-Length': data.length     } }; const req = https.request(options, (res) => {     let data = '';     res.on('data', (chunk) => {         data += chunk;     });     res.on('end', () => {         console.log(JSON.parse(data));     }); }).on("error", (err) => {     console.log("Error: ", err.message); }); req.write(data); req.end();

      options對象中的protocols和`port'屬性是可選的。

      PUT和DELETE請求

      PUT和DELETE請求格式與POST請求類似。 只需將options.method值更改為PUT或DELETE。

      這是DELETE請求的示例:

      const https = require('https'); const options = {     hostname: 'reqres.in',     path: '/api/users/2',     method: 'DELETE' }; const req = https.request(options, (res) => {     // log the status     console.log('Status Code:', res.statusCode); }).on("error", (err) => {     console.log("Error: ", err.message); }); req.end();

      推薦學習:《nodejs視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號