简体   繁体   English

如何使用 node.js GOT http 请求库进行故障排除?

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

I have some code using GOT querying a graphQL endpoint:我有一些使用 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);
    } 

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

How do I see what GOT is sending out to the GQL endpoint?如何查看 GOT 发送到 GQL 端点的内容? FYI, this call works fine in the GQL console and also in Postman.仅供参考,这个调用在 GQL 控制台和 Postman 中都可以正常工作。

The got() library has hooks that allow you to see the headers it's about to send. got()库有钩子,可让您查看它即将发送的标头。 Here's an example that you can run and then insert the same thing into your code:这是一个示例,您可以运行然后将相同的内容插入到您的代码中:

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);
});

You can also get a network analyzer like Wireshark to put on your client computer and watch the actual network traffic.您还可以使用Wireshark之类的网络分析仪安装在您的客户端计算机上并观察实际的网络流量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM