Re: $resource calls with Basic Authentication required, how can I do that?

2,796 views
Skip to first unread message

Roberto Martínez

unread,
Oct 10, 2012, 10:39:10 AM10/10/12
to ang...@googlegroups.com
Hi Rameesh

I wonder if you figured the solution of this. I'm trying to build something like you mentioned. Thanks!



On Saturday, July 7, 2012 7:20:49 PM UTC-4, Rameesh wrote:
Thank you very much for the direction, that helped.

I'm now looking for changing the header at runtime. For instance, a user enter a name/password pair and I need to pass that through a header before I hit the $resource call. Any ideas on that?

Rameesh

On Saturday, July 7, 2012 6:09:53 PM UTC+2, Noah Freitas wrote:
I can't answer your Basic Authentication question, but I did recently have to set a custom header on all $resource calls from my application:

    .config(['$httpProvider', function($httpProvider) {
      $httpProvider.defaults.headers.common['X-Auth'] = apiKey;
    }]);

The $httpProvider.defaults.headers object is probably where you are going to find your solution.  The common object will set headers for all $http requests, though you can be more granular and set them for specific methods as well (GET, POST, etc.) 

The documentation can be found here: http://docs.angularjs.org/api/ng.$http under "Setting HTTP Headers".

On Friday, July 6, 2012 6:59:34 PM UTC-7, Rameesh wrote:
How can I use the $resource service to call RESTful resources that requires Basic Authentication? How can I pass the user/password? Any links/posts/ideas are really appreciated.

A more general question: How I can define the Request Headers while using $resource?

Forgive the newbie questions. A couple of weeks and you'll find me answering similar questions myself :)

Rameesh

sep...@gmail.com

unread,
Oct 23, 2012, 3:28:39 AM10/23/12
to ang...@googlegroups.com
+1 from me on this.

Requests with credentials are only mentioned once in Angular's $http docs:

- Tom

Dejan Strancar

unread,
Dec 5, 2014, 9:33:28 AM12/5/14
to ang...@googlegroups.com
Ive found a way to easily make basic authentication via $resource.
First you need to get Authentication key from API, so you should call $http POST(with your username/password) to server.
If return is success just get authentication key as return, fill $http.defaults.headers.common.Authorization variable with that key.
You can do $resource as it is described everywhere call after that and it will work since $http is dependency of $resource.

Hope that helps.

Francisco Javier Urbano

unread,
Oct 2, 2015, 7:47:21 PM10/2/15
to AngularJS
Dejan Strancar have the example ?

Dejan Strancar

unread,
Oct 7, 2015, 5:21:37 AM10/7/15
to AngularJS
Reply all
Reply to author
Forward
0 new messages