-2

enter image description here

I'm working with node-fetch in vscode. I'm wondering if there is a way to inspect the request after the response has been obtained. I can set a breakpoint at the last line (arrow),

  const r = await fetch("MYURL", {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "en-US,en;q=0.9",
            "content-type": "application/json;charset=UTF-8",
            "requestverificationtoken": requestverificationtoken,
            "sec-ch-ua": "\"Not?A_Brand\";v=\"8\", \"Chromium\";v=\"108\", \"Google Chrome\";v=\"108\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Chrome OS\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "cookie": "ASP.NET_SessionId=aonfqye5wi1k032y0mvgmbsj",
            "Referrer-Policy": "strict-origin-when-cross-origin"
        },
        "body": "{\"Number\":\"110067270\"}",
        "method": "POST"
    });
    const body = await r.json()
    ---->  console.log(body);

There are multiple sources that refer to adding :

NODE_DEBUG = 'https'

to the code, but when I try to add this to my launch.json file nothing happens and the linter indicates an error.

Is there anyway to combine the node-debug =https and the built in node debugging of VScode?

Edit:

enter image description here

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • The "linter error" is because single quotes aren't valid JSON delimiters. Beyond that I'm not entirely sure what the problem is - when you set that breakpoint, do you not get access to the variable `r`? – jonrsharpe Jan 17 '23 at 17:55
  • I switched to double quotes, It now says:' Property NODE_DEBUG is not allowed.' I do get access to r, but I can't see the original request. In order to debug, I want to see what is being sent to the server. – user1592380 Jan 17 '23 at 20:27

1 Answers1

1

I've tried just run the fetch on it's own to check if there's something wrong with it and this is my test

import fetch from 'node-fetch'

const run = async () => {
    const r = await fetch("https://randomuser.me/api/")
    const json = await r.json()
    console.log(json)
}

run();

and I get a response without any issues

enter image description here


and I also added Debug and everything run correctly with just the default NodeJs configuration

{
    "type": "node",
    "request": "launch",
    "name": "Launch Program",
    "skipFiles": [
        "<node_internals>/**"
    ],
    "program": "${workspaceFolder}/index.mjs"
}

enter image description here


also, keep in mind that you add env variables to the config via the env property like you see in this SO Answer

balexandre
  • 73,608
  • 45
  • 233
  • 342
  • Thank you, that really helps. Are you able to see the request, after getting the response. I'd like to know what the server is getting. – user1592380 Jan 18 '23 at 02:22
  • "Are you able to see the request, after getting the response" - what do you mean by "request", the information that you send to the server? like headers? `fetch()` returns an object of type [`Response`](https://github.com/node-fetch/node-fetch#class-response) so all you get in your `r` variable is the response (you some [properties you can inspect](https://i.stack.imgur.com/2BSIk.png)), the request object is not accessible, you can read more in [the docs](https://github.com/node-fetch/node-fetch#class-request) with some examples – balexandre Jan 19 '23 at 21:07
  • Yes , I am asking about the information such as headers, cookies sent to the server. If you look in devrools (as in scrrenshot above) - you can get request headers etc. How do you get this? – user1592380 Jan 24 '23 at 16:05
  • as I wrote in the comment above, you only get the Response headers, not the Request headers... the output of the fetch call is only the Response object, not the request like `axios` allows you to have – balexandre Jan 30 '23 at 09:20
  • Ok - Thats very helpful, So I should use axios instead - if I need the request info – user1592380 Feb 03 '23 at 02:08