iamjb
September 10, 2015, 7:20am
1
Hi,
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) {
$http.post(<url>, <post object>)
.success(function (response) {
console.log("success: " + JSON.stringify(response));
callback();
}).error(function (data, status, headers, config) {
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
callback({});
});
}
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:
...
$http
.post(<url>,
<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.
iamjb
September 14, 2015, 4:17am
3
@juliosampaio
Thx a lot.
U solved my problem.
1 Like
Hi
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?
tumbas
July 21, 2016, 9:43am
5
saleem1234:
Hi
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
…
$http
.get(,
{
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(“http://192.168.1.101:8081/atcsweb/rest-service/assoc-shift/isShiftExit/ ”+$rootScope.associateId, {headers: {‘Authorization’: 'Bearer '+$rootScope.token}})
.success(function(res){
})
.error(function(res){})
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.post = {}; $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