簡體   English   中英

如何使用 node.js GOT http 請求庫進行故障排除?

[英]how to troubleshoot using the node.js GOT http request library?

我有一些使用 GOT 查詢 graphQL 端點的代碼:

// set up params for call to weather cache 
    const queryQL = `
      query weather {
        weather(where: {idLatLong: {_eq: "${latLong}"}}) {
          id
          idLatLong
          updated_at
          lat
          long
          requestedByUserId
          data
          created_at
        }
      }
    `
    const query = {query: queryQL};
    const options = {
      headers: {
        'X-Hasura-Admin-Secret': process.env.HASURA_KEY
      },
      responseType: 'json'
    }

    // see if there's an existing record for the lat long
    try {
      const response = await got.post(process.env.GQL_ENDPOINT, query, options);
      console.log('query weather hasura');
      console.log(response.body);
    } catch(error) {
      console.log(error);
    } 

我收到 Hasura {"errors":[{"extensions":{"path":"$","code":"invalid-headers"},"message":"Missing Authorization header in JWT authentication mode"}]}

如何查看 GOT 發送到 GQL 端點的內容? 僅供參考,這個調用在 GQL 控制台和 Postman 中都可以正常工作。

got()庫有鈎子,可讓您查看它即將發送的標頭。 這是一個示例,您可以運行然后將相同的內容插入到您的代碼中:

const got = require('got');

got("http://www.google.com", {
    hooks: {
        beforeRequest: [function(options) {
            console.log(options);
        }]
    }
}).then(result => {
    let i = 1;
}).catch(err => {
    console.log(err);
});

您還可以使用Wireshark之類的網絡分析儀安裝在您的客戶端計算機上並觀察實際的網絡流量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM