Here is actual code that works well but i would like to check if my headers are well transmitted to my api:
var request = require('request');
var express = require('express');
var router = express.Router();
/* GET data by sportId */
router.get('/:locale/:sportId/:federationId/:date', function(req, res) {
var date = req.params.date;
var locale = req.params.locale;
var sportId = req.params.sportId;
var federationId = req.params.federationId;
request(getEventsOptions(sportId, federationId, date, locale), function(error, response, body) {
res.send(body);
});
});
// get options for request
function getEventsOptions(sportId, federationId, date, locale)
{
return {
url: `http://myapi.com/event/sport/${sportId}/date-from/${date}`,
headers: {
'accept': 'application/json',
'dateTo': date,
'federationIds': federationId,
'X-Application-ID': 'sporter',
'Accept-Language': locale,
}
};
}
So my question is quite general, how can i check headers of my call in a node js app ?
Generally talking, you can retrieve your headers with the inject request object as in var xtoken = req. headers['x-token']; . In your case, you could try const authorization = req. headers['authorization'] to retrieve Authorization header.
The header tells the server details about the request such as what type of data the client, user, or request wants in the response. Type can be html , text , JSON , cookies or others.
In the terminal type node --max-http-header-size=1024 server. js . If you now open URL http://127.0.0.1:9000 in browser, you should see updated value of HTTP headers size.
There are three ways to do this:
First, using req.get function:
req.get('headerName');
Second, using req.header function:
req.header('headerName');
Third, using req.headers actual object:
req.headers['headerName'];
I hope it helps you.
According documentation you need req.get function. Also you can use req.headers object with all sended headers.
Code example:
const request = require('request');
const express = require('express');
const router = express.Router();
router.get('/:locale/:sportId/:federationId/:date', (req, res) => {
// destructuring assignment for better readability
const { date, locale, sportId, federationId } = req.params;
// header example with get
const authHeader = req.get('Authorization');
console.log(authHeader);
// example with headers object
console.log(req.headers);
request(getEventsOptions(sportId, federationId, date, locale), (error, response, body) => {
res.send(body);
});
});
function getEventsOptions(sportId, federationId, date, locale) {
return {
url: `http://myapi.com/event/sport/${sportId}/date-from/${date}`,
headers: {
'accept': 'application/json',
'dateTo': date,
'federationIds': federationId,
'X-Application-ID': 'sporter',
'Accept-Language': locale,
}
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With