Questions tagged [angular-http]

AngularJS tag for $http service is a core Angular service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP.

The $http service is a core Angular service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP.

For unit testing applications that use $http service, see $httpBackend mock.

For a higher level of abstraction, please check out the $resource service.

The $http API is based on the deferred/promise APIs exposed by the $q service. While for simple usage patterns this doesn't matter much, for advanced usage it is important to familiarize yourself with these APIs and the guarantees they provide.

General usage

The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise with two $http specific methods: .then and .catch.

// Simple GET request example :
$http.get('/someUrl')
  .then(function(response) {
    var data = response.data;
    // this callback will be called asynchronously
    // when the response is available
}).catch(function(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});
// Simple POST request example (passing data) :
$http.post('/someUrl', {msg:'hello word!'})
  .then(function(response) {
    // this callback will be called asynchronously
    // when the response is available
}).catch(function(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

Since the returned value of calling the $http function is a promise, you can also use the then method to register callbacks, and these callbacks will receive a single argument – an object representing the response. See the API signature and type info below for more details.

A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the error callback will not be called for such responses.

970 questions
15
votes
2 answers

Handling Expired Token From Api in Angular 4

I need help in handling expired token in my angular application. My api has the expired time but my problem is when i forgot to log out of my angular application, after some time, i still can access the homepage but without data. Is there something…
Joseph
  • 7,042
  • 23
  • 83
  • 181
14
votes
5 answers

Angular 4 How to return multiple observables in resolver

As the title states, I need to return multiple observables or maybe results. The goal is basically to load let's say a library list and then load books based on that library IDs. I don't want to call a service in components, instead I want all the…
hxdef
  • 393
  • 2
  • 6
  • 15
14
votes
2 answers

How to add HttpClient Interceptors conditionally in Angular

Recently I have been using Interceptors with Angular HttpClient. I add headers corresponding to some HTTP GET methods and for some I do not need those headers. How can I tell my interceptor to conditionally add interceptors to only those methods? I…
14
votes
2 answers

Angular alternative $http

In AngularJS for sending a request I use the builtin $http service. What shall I use for sending a request to a server in Angular? I can't find any doc that covers the subject.
Eugene
  • 1,690
  • 3
  • 16
  • 30
14
votes
3 answers

Reject from 'response' into 'responseError'

Sometimes, the API I'm using will return 200 ok even though there has been an error. The response JSON object will look something like: { error: true } I've built a $http response interceptor that simply checks for this error and rejects it. I…
Rob Campion
  • 1,359
  • 13
  • 26
12
votes
3 answers

Set Cookie in Request Headers Angular2

I am new to angular2. My server(spring) responds authentication with a set-cookie value in its response headers. How to set that cookie to the request headers for the next API calls? I searched a lot, but I cannot find a suitable solution.
Sibiraj
  • 4,486
  • 7
  • 33
  • 57
12
votes
2 answers

Accessing custom http response headers in angularjs

I am trying to access the header 'error-detail' as you can see in the browser network inspector (link above), the header gets returned. Server-wise I have also added the custom header to the 'Access-Control-Expose-Headers' to allow cross-domain…
Ibrahim Farah
  • 133
  • 1
  • 7
12
votes
3 answers

What is the HTTP promise object in AngularJS?

Although I am working with the HTTP promise object in AngularJS, I don't have a clear concept of what an HTTP promise object actually is and what the is difference between an HTTP promise object and a traditional object in AngularJS! Would anybody…
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
12
votes
2 answers

angularjs $http.get to get json not working in the service layer

I am developing an angularjs app as a part of my angularjs learning. I have controllers and from there I am calling service layers. leagueManager.service("teamsService", function($http){ var teams = {}; …
cooler
  • 753
  • 4
  • 9
  • 18
11
votes
4 answers

No methods in http response object

Object returned by $http.get has no methods. Example: I have my class model export class Lab { constructor( public id: number, public name: string, public description: string, public isActive: boolean, …
kriss
  • 976
  • 17
  • 27
11
votes
2 answers

$http response interceptors headers

I'm sending a custom header from server in a response. In $http response interceptor I want to get this header, but the only header I could get is Content-type header. How can I resolve my problem? Piece of my $http interceptor: response: function…
Maniek Stasz
  • 347
  • 3
  • 13
10
votes
1 answer

Angular 6 HttpClient return instance of class

Before angular's new HttpClient was introduced, our objects returned from the http api call could be validated with the instanceof keyword. they no longer can with the HttpClient Module. I'm trying some simple methods but the type checks return…
Scott Clark
  • 608
  • 6
  • 21
10
votes
8 answers

How to Show spinner for every HTTP requests in angular 5?

i am new to angular 5 . How to code a common function to show spinner for every HTTP request in angular 5.Please help me to implement this.
10
votes
2 answers

Overload request headers and params with HttpClient get

In HttpClientModule, is there a method to pass headers and params to get request. import { HttpHeaders, HttpParams, HttpClient } from @angular/common/http'; const headers = { headers: new HttpHeaders({}) } let params = new HttpParams({…
10
votes
2 answers

$http.post() method is actally sending a GET

NOTE: I've found a possibly related issue that warrants a new question here This is a weird problem. I've been using angular over the course of 2 years and have never run into this problem. I'm using angular v1.5.0. I'm making a post request like…
dopatraman
  • 13,416
  • 29
  • 90
  • 154
1 2
3
64 65