0

I am want to connect api with mvc site using oauth authentication. for this, i need to send access token through http request as below.

addPostComment: function addPostComment(postid, UserId, comment, accessToken) {

                var request = $http({
                    method: "post",
                    url: apiPath.addPostComment,
                    data: {
                        SocialPostId: postid,
                        CommentDescription: comment,
                        CommentedUserId: UserId,
                        CommentedDate: new Date()
                    },
                    params: {
                        action: "post"
                    },                   
                    beforeSend: function (xhr) {

                        xhr.setRequestHeader('Authorization', 'bearer ' + accessToken);
                    }
                });
                return (request.then(handleSuccess, handleError));
            }

parameter accessToken have value.but while i set as request header, it always says Authrization: Bearer undefined.

Can anyone tell me what is wrong in this code?

Lalitha
  • 67
  • 1
  • 11
  • May be you can directly say headers: {'Authorization' : 'bearer ' + accessToken } instead of adding them in beforeSend.. – G_S Apr 15 '16 at 04:57
  • http://stackoverflow.com/questions/22140591/what-is-the-equivalent-of-jquery-ajax-beforesend-in-angularjs as far as i understand, you can't use beforeSend in angularjs "$http" without an interceptor. – adem caglin Apr 15 '16 at 08:01

1 Answers1

0
    $http({
      method: "post",
                url: apiPath.addPostComment,
                data: {
                    SocialPostId: postid,
                    CommentDescription: comment,
                    CommentedUserId: UserId,
                    CommentedDate: new Date()
                },
                params: {
                    action: "post"
                }, 
      headers: {
      'Authorization': 'bearer ' + accessToken
      }
    });

You Can send HTTP requests like this ...

XAVIER K.I
  • 349
  • 2
  • 9
  • yes it is working fine. but i want to update header in request before it send, so I am using this method. in this code, apipath is constant. angular.module("test").constant('apiPath', {'accessToken': "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1laWQiOiIxIiwidW5pcXVlX25hbWUiOiJzdXBlcmFkbWludX". if I set accesstoken in this constant and used in service as beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'bearer ' + apiPath.accessToken); }. it is working fine. why it is not working if i use param token and why it is working if i use constant token. – Lalitha Apr 15 '16 at 06:28
  • Hi Adem, actually I declared const bearerToken=accessToken and use it in beforeSend function, it is working fine only for postmethod. if I use the same concept in get method, it is not working. http request send authorization as Bearer undefined. any one know what is actual issue in this and how to solve this? – Lalitha Apr 15 '16 at 09:28
  • if you want to update the header just use a service to get the Access token , and get the access token by calling the Method in your code, ` headers: {` `'Authorization': 'bearer ' + yourService.getAccessToken()` `}` and add yourService as a dependency , will solve your problem .. so you will get the updated access token in all your requests.. – XAVIER K.I Apr 15 '16 at 10:17