【问题标题】:how to troubleshoot using the node.js GOT http request library?如何使用 node.js GOT http 请求库进行故障排除?
【发布时间】:2020-07-21 12:49:36
【问题描述】:

我有一些使用 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 中都可以正常工作。

【问题讨论】:

    标签: javascript node.js graphql hasura node.js-got


    【解决方案1】:

    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 之类的网络分析仪安装在您的客户端计算机上并观察实际的网络流量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多