Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check headers in nodejs?

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 ?

like image 381
yoyojs Avatar asked Sep 19 '18 15:09

yoyojs


People also ask

How do I validate headers in node js?

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.

What are headers in node js?

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.

How do I see node header size?

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.


2 Answers

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.

like image 96
Mahan Avatar answered Oct 10 '22 19:10

Mahan


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,
    }
  };
}
like image 43
galkin Avatar answered Oct 10 '22 17:10

galkin