2

I have a REST web service I am trying to consume in my AngularJS v1.5 project. I am trying to pass authorization value to the header but I am getting access denied error. I tried options like the one below

$http.defaults.headers.common['Authorization'] = 'Basic encoded_credentials';

Nothing works for me. REST web service is deployed in Tomcat and the URL is in format.

Any help is highly appreciated

0

2 Answers 2

2

I don't know what you are trying to achieve here but simple code is

var httpHeader = {
  headers: {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "Authorization": yourAuthKey,
  },
};
$http.get(yourUrl, httpHeader);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you quick response. I tried the snippet but I am still getting the following error. OPTIONS servername:8280/dctm-rest/repositories/swbc_bpm 401 (Unauthorized) XMLHttpRequest cannot load servername:8280/dctm-rest/repositories/swbc_bpm. Response for preflight has invalid HTTP status code 401
@xHulk I think you are facing CORS issues, I suggest you look into it on the api side.
0

To perhaps improve your question:

How are you storing the credentials? Is this cookie based? if so try adding the following line inside your Angular config:

$httpProvider.defaults.withCredentials = true;

(You will need to inject and require $httpProvider in the config

If this is not what you are doing, can you offer insight in how you are managing saving your authentication details. Perhaps you need to write some custom middleware to inject the proper headers into every request.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.