How to add header param on Restful API call with ionic $http service?


How to add header param on Restful API call with ionic $http service?

I hv googling for while…and cannot not find the way to add header param on HTTP call…

I only got the way to post with data as follow…

function PostAPICall(<post object>, callback) {
            $<url>, <post object>)
                    .success(function (response) {
                      console.log("success: " + JSON.stringify(response));
                    }).error(function (data, status, headers, config) {

Then…How to add header param on Restful API call with ionic $http service?

Thanks for your help.

Hi @iamjb,

You can pass a config in the third parameter, so your code would be something this:

        <post object>,
          headers: {'Authorization': 'Basic QWxhZGRpbjpvcJSuIHNlc2FtZQ=='}

Actually $http is an AngularJS service, this way you can have a look at the docs for more information.

Thx a lot.
U solved my problem. :grin:

1 Like


I have tried this code but iam getting error

$http.get(SERVER.url+“assoc-triplog/isTripExit/”+$rootScope.associateId, {headers: {‘Authorization’: 'Bearer '+$rootScope.token}})

with 500 status code

how can i solve it?

500 is Internal Server Error, please check your API server response. You can test with Postman tool.

Thanks for your Reply,

I have tried in post man still iam getting an error, the server is showing that the token is not reached to server…

Hi @saleem1234,

You should not pass the $http.get("....") in the GET field, you should pass just your URL, the value inside $http.get("...."), that’s why you are getting this error, 'cause $http.get("....") is not a valid URL.

Thank you @juliosampaio

That is ok for POST MAN, can you please help me to call the same service from my IONIC app…
I have tried the solution that you have posted above but it’s giving error

headers: {‘Authorization’: ‘Basic QWxhZGRpbjpvcJSuIHNlc2FtZQ==’}

i am getting this error in the browser “Response for preflight has invalid HTTP status code 500”

Would you post your code here? This way will be easier to help you.

ok @juliosampaio
wait a min…

$http.get(“”+$rootScope.associateId, {headers: {‘Authorization’: 'Bearer '+$rootScope.token}})


here is the error msg snapshot in console

Hi @saleem1234,

Try doing this in your app.config:

app.config(function ($httpProvider) { $httpProvider.defaults.headers.common = {}; $ = {}; $httpProvider.defaults.headers.put = {}; $httpProvider.defaults.headers.patch = {}; });

For more details, refer to this S.O. post

Thanks for your reply @juliosampaio

Do i need to install any Plugins to work with this?

No, this is the built-in $httpProvider