2021年6月16日 星期三

[request, express] How to show the request headers infomation for debugging

How to show the request headers infomation for debugging

井民全, Jing, mqjing@gmail.com


There are two ways to show the HTTP request headers. Note that restart the Browser before debugging the server.

1. Client site (on Browser)

1.1. Firefox

That's [Inspect] -> [XHR], then debug.

Step 1: Open the [Inspect]

Fig. Launch the Inspect window. (Edit)

Step 2: Turn on the [XHR] 

Fig. The XHR option.

Step 3: Start to debug

Fig. Check the XHR tag.

2. Server site (in route module)

The following snippet shows you how to do this in a routing module.  

console.log(JSON.stringify(req.headers));


2.1. Code

File: myAPI.ts

import express, {Request, Response} from 'express';


var router = express.Router();


router.get('/', function(req:Request, res:Response, next) {

    console.log(JSON.stringify(req.headers));  // print request headers 

    res.header("Access-Control-Allow-Origin", "*");

    res.send('The statusAPI was invoked.');

});


module.exports = router;



2.2. Result

{"host":"192.168.21.14:9000","user-agent":"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0","accept":"*/*","accept-language":"en-US,en;q=0.5","accept-encoding":"gzip, deflate","referer":"http://localhost:3000/","origin":"http://localhost:3000","dnt":"1","connection":"keep-alive"}