最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to check debug logs for node-fetch in node? - Stack Overflow

programmeradmin1浏览0评论

I have the script below that makes an API call to a public internet service from behind a corporate proxy. However, the script is hanging at console.log("Started API Call"). How can I check the debug logs of the fetch call?

I added our proxy using the environment variables below in Linux.

  • HTTP_PROXY
  • HTTPS_PROXY
  • npm_config_http_proxy
  • npm_config_https_proxy

When I run the API call through curl, it is successful, but I am unable to make it work through Node.js. How can I run the script in debug mode and see the logs of the API call being made using the node-fetch library?

import fetch, * as fetchothers from "node-fetch";

const pat = "1234"

const url = ";

const options = {
   method: "GET",
   headers: {
      Authorization: `Bearer ${pat}`
   }
}

try {
   console.log("Started API Call");
   const response = await fetch(url, options);
   const data = await response.json();
   console.log(data);
} catch (error){
   console.error(error);
}

I have the script below that makes an API call to a public internet service from behind a corporate proxy. However, the script is hanging at console.log("Started API Call"). How can I check the debug logs of the fetch call?

I added our proxy using the environment variables below in Linux.

  • HTTP_PROXY
  • HTTPS_PROXY
  • npm_config_http_proxy
  • npm_config_https_proxy

When I run the API call through curl, it is successful, but I am unable to make it work through Node.js. How can I run the script in debug mode and see the logs of the API call being made using the node-fetch library?

import fetch, * as fetchothers from "node-fetch";

const pat = "1234"

const url = "https://example.com/users"

const options = {
   method: "GET",
   headers: {
      Authorization: `Bearer ${pat}`
   }
}

try {
   console.log("Started API Call");
   const response = await fetch(url, options);
   const data = await response.json();
   console.log(data);
} catch (error){
   console.error(error);
}
Share Improve this question asked 9 hours ago nooneninenoonenine 1093 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

To debug node-fetch in Node.js behind a proxy:

1. Enable Debug Logs

Run your script with:

NODE_DEBUG=http,https node your_script.js

2. Use Debug Module

Install and modify your script:

npm install debug
import fetch from "node-fetch";
import debug from "debug";

const log = debug("fetch:log");
log("Started API Call");

const response = await fetch("https://example.com/users", {
   method: "GET",
   headers: { Authorization: `Bearer 1234` }
});

log("Response:", response.status);
console.log(await response.json());

Run with:

DEBUG=fetch:log node script.js

3. Set Proxy Manually

If needed, set a proxy:

import { HttpsProxyAgent } from "https-proxy-agent";
const agent = new HttpsProxyAgent("http://your.proxy.com:8080");
const response = await fetch(url, { method: "GET", agent });

4. Ignore SSL Issues (If Needed)

NODE_TLS_REJECT_UNAUTHORIZED=0 node script.js

I suggest use "request" module instead of "node-fetch".

   request(url, (err, response, body)=>{
    if(err){
        console.log(err);
    }else if (response.statusCode !== 200){
        console.log(`Request faild : Status code is ${response.statusCode}`);
    }else{
        console.log(body);
    }
   })

above is sample code for use request. Regards.

发布评论

评论列表(0)

  1. 暂无评论