Lint
Lint

Reputation: 935

Send object in response headers Node JS

I need to send an object in my response headers, but when I access it I only see [object object] no error and nothing else. What could possibly be the problem ? here's how I'm sending from my server

pagination = {
    'pageSize': 25,
    'someInformation': 'blablabla...'
}
res.header('Access-Control-Expose-Headers', 'X-Pagination');
res.setHeader('X-Pagination', pagination);

enter image description here

Upvotes: 1

Views: 2252

Answers (1)

Aritra Chakraborty
Aritra Chakraborty

Reputation: 12552

So header values are strings. When you pass an object to a function that needs string it uses the Object.toString() property and uses it.

const obj = {a:1}
obj.toString() // outputs "[object Object]"

In order to pass JSON you need to use JSON.stringify In your case it will be:

pagination = {
    'pageSize': 25,
    'someInformation': 'blablabla...'
}
res.header('Access-Control-Expose-Headers', 'X-Pagination');
res.setHeader('X-Pagination', JSON.stringify(pagination));

This is current output:

enter image description here

Upvotes: 2

Related Questions