A
A
Artem Prokhorov2021-06-11 11:39:14
JavaScript
Artem Prokhorov, 2021-06-11 11:39:14

Why is the Bearer token not being sent to the server via fetch?

This is my fetch

let token    = 'bt8lIQORNofZ3Kd11bACdpl6xqZt87eY;
        let headers = new Headers();
        headers.set('Authorization', 'Bearer ' + token);
        fetch('http://back.ru/menu?tree=1', {
            credentials: "include",
            headers:  {
                headers
            },
        })
            .then(response => response.json()).then(data => { context.commit('SET_LIST', data) });


If I make a request in Postman via Bearer auth adding a token, then everything works, but for some reason it doesn’t work via fetch. What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Prokhorov, 2021-06-11
@kotcich

As a result, I found out that the server does not see the headers if they were sent through the object new Headers()
, and when I added the headers manually, like this:

headers:  {
                'Authorization':  'Bearer ' + token
            },

then the server has already begun to see the sent token.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question