JavaScript react + redux - 401 - unauthorized - missing headers in Request Headers react redux redirect unauthorized

return fetch(`{SERVICE API URL}`, {
  method: 'GET',        
  headers: {
    'userName': "username",
    'password': "password",
    'content-type': 'application/json'
  }
})
.then(response => response.json())
.then(json => dispatch(receivePosts(reddit, json)))

I'm trying to get service API data with authorization headers, but getting 401 - Unauthorized error and the response is Missing Request Headers.

Tried with sending authorization content with body also - getting same error 401 - Unauthorized error.

Edited:

enter image description here

 headers: {
        'userName': "xyz",
        'sessionToken': "xyz................."
      }

When I'm checking with Postman client it is working fine, but not with the redux-saga fetch method. Kindly help me for this.

Answer:1

Looks like it's a backend problem - CORS Filter configuration

If the backend is on a different server (could be on the same machine, but in a different Application Server, in other words, on a different port) you have to do some CORS Filters configurations.

The frontend code is running on a server - that means it's an application. Postman is a client, just like Google Chrome or any other browser. That's the explanation why you can do the request without any problem from Postman but unsuccessful from your frontend application.

I guess you enabled the Access-Control-Allow-Origin header on the backend

Now you have to allow your custom headers with Access-Control-Allow-Headers

Answer:2

Whenever I use fetch and I need to add headers to the request I do it this way:

headers: new Headers({
  Accept: 'application/json',
  Authorization: token,
  'Content-Type': 'application/json',
}),

so you might want to try this approach, also in order to debug this issue you might want to check your Netowrk tab and verify which headers are sent with the request.

Answer:3

You need to add an Authorization bearer header.

For instance:

headers = new Headers({
    'Authorization': `Bearer ${authorizationCodeOrCredentials}`
});

In your code:

return fetch(`{SERVICE API URL}`, {
  method: 'GET',        
  headers: {
    'Authorization': 'Bearer ' + someValue, // Add this line
    'userName': "username",
    'password': "password",
    'content-type': 'application/json'
  }
})
.then(response => response.json())
.then(json => dispatch(receivePosts(reddit, json)))
Answer:4

I'm struggling to get the content of the tag <text> $( "svg" ).click(function(e) { console.log("we are in the svg element"); //console.log(e.target.next("text")); }); <...

I'm struggling to get the content of the tag <text> $( "svg" ).click(function(e) { console.log("we are in the svg element"); //console.log(e.target.next("text")); }); <...

  1. text case converter
  2. casetext
  3. text case css
  4. text case changer
  5. text case change
  6. text case randomizer
  7. text case in excel
  8. text case types
  9. text case online
  10. text case alternator
  11. text cases and materials on contract law
  12. text case in word
  13. text case app
  14. text case change online
  15. text case converter online
  16. text case transform
  17. text cases and materials on criminal law
  18. text case change in excel
  19. text cases in ms word
  20. text case convertor

I'm building an interface for a webmail: I receive the content of the mail, I put it in a <div> element where it display the message. The problem is that every mail has the <style> tag ...

I'm building an interface for a webmail: I receive the content of the mail, I put it in a <div> element where it display the message. The problem is that every mail has the <style> tag ...

i am creating a youtube chrome extension and try to load extension from background page automatically when any youtube video or channel page open but extension will not load untill i refresh same page ...

i am creating a youtube chrome extension and try to load extension from background page automatically when any youtube video or channel page open but extension will not load untill i refresh same page ...

  1. chrome extension will not stay enabled
  2. chrome extension will not remove
  3. chrome extension will not install

I have result array on script like this: var myurlsave = [{"url_field":"yahoo","enabled":true},{"url_field":"google","enabled":true},{"url_field":"bing","enabled":true}]; I want to extract it to ...

I have result array on script like this: var myurlsave = [{"url_field":"yahoo","enabled":true},{"url_field":"google","enabled":true},{"url_field":"bing","enabled":true}]; I want to extract it to ...