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
4
votes
1 answer

Cors policy error when use Angular 7 post on ionic 4

I'm upset to post similar problem but spend more than 3 days and couldn't solve the problem. Platform: Ionic 4 Angular 7 When I try to Post I have cors policy Error. Access to XMLHttpRequest at 'https://mersinders.com/api/service.php?run=giris' from…
abdullah
  • 171
  • 1
  • 2
  • 7
4
votes
3 answers

Getter Methods on Deserialized Objects in Typescript

We have a variety of Http calls to our services that return a collection of objects. In our angular application, typescript interprets these into the appropriate type if specified, for example Person: export class Person{ public firstName:…
Marshall Tigerus
  • 3,675
  • 10
  • 37
  • 67
4
votes
0 answers

Download a file using HttpClient in Ionic 3

I need to download a file from server. I previously used FileTransfer plugin of Ionic and it worked fine. I can also get the download progress with size. this.fileTransfer.download(url, savePath, false, options).then((entry) => { …
Joseph
  • 1,060
  • 3
  • 22
  • 53
4
votes
4 answers

Angular : Using the equivalent of RequestOptions of Http with HttpClient

i'm migrating from Http to HttpClient I'm used to add some headers to my http requests like the following with : import { RequestOptions, Request, RequestMethod, Headers } from '@angular/http'; this.pass = btoa(cuid + ': '); this.pass = "Basic "…
firasKoubaa
  • 6,439
  • 25
  • 79
  • 148
4
votes
4 answers

Angular5 HttpClient, set variables after GET api call

I have a service (lets call it MySharedService) which multiple components use. Inside MySharedService, I call another service that makes API calls. MySharedService holds a JavaScript object that is assigned after my GET call. My problem is that my…
o.o
  • 3,563
  • 9
  • 42
  • 71
4
votes
3 answers

Angular Testing Getting Actual HTTP Response

I'm new to angular unit testing. What I want to do is getting actual results from my API. I checked this documentation but as I understand, I should create mock responses. Here is my code, myService.ts: ... public GetAll = (apiname: string):…
onurcanak
  • 109
  • 1
  • 9
4
votes
1 answer

Http.post not working when 'application/json' set in header in Angular4 cli

I am beginner in the Angular CLI, I have used the login api:'http://localhost/appointjobs/index.php/admin_api/index' using http.post but, I didn't get the post data in server side(codeigniter/php) when set 'content-type:application/json'. Below…
Mandip Vora
  • 309
  • 1
  • 2
  • 14
4
votes
1 answer

Make parallel calls http get or post calls in angular 2

How to make parallel calls HTTP get or post calls in angular 2? I have 2 service calls on the response of one callus have to make another call. Could some suggest me how to call make these parallel calls with error handling scenarios?
4
votes
1 answer

Angular HttpClient params with method GET do not work

I wanna ask you about params with get method. I have something like this: path = 'https://example.com/api'; const params = new HttpParams(); params.append('http', 'angular'); return this.http.get(path, {params: params}); I thought I…
Adam Adamski
  • 737
  • 3
  • 11
  • 20
4
votes
4 answers

Angular 4 this.http.get(...).map is not a function

Hi i have problem with my code. import { Injectable } from '@angular/core'; import { Car } from "./models/car"; import { Observable } from "rxjs/Observable"; import { Http } from "@angular/http"; import 'rxjs' @Injectable() export class…
Marek Patyna
  • 87
  • 1
  • 6
4
votes
1 answer

Angular 2/4 & RxJS - Subscriptions within forEach - Ensuring flow control doesn't continue until all Observables are complete

The goal is to iterate through a collection of IDs, making an HTTP call for each ID. For each ID, I'm using a service with a get() method that returns an Observable. Each time the get() method is called, I'm subscribing to the returning Observable…
Steve Boniface
  • 571
  • 1
  • 11
  • 23
4
votes
0 answers

Property 'json' does not exist on type 'HttpResponse'

HttpResponse is a generic class in @angular/common/http, so I need to mention a type e.g. HttpResponse (replace 'T' with data type). Now that I am replacing old angular code from this: .map((res: Response) => res.json()); to this: .map((res:…
Ankur Shah
  • 801
  • 1
  • 12
  • 26
4
votes
2 answers

Angular >= 4.3, httpClient.get params empty

I'm trying to migrate my Http requests to HttpClient requests. I was able to migrate my post queries but I'm facing a problem while migrating get queries. When I do so, my backend doesn't receive any parameters respectively, it tells me that the…
David Dal Busco
  • 7,975
  • 15
  • 55
  • 96
4
votes
1 answer

How to POST binary files with AngularJS (with upload DEMO)

unable to send file with angular post call I am trying to post .mp4 file with some data through ionic 1 with angular 1. While posting through POSTMAN it is fine and working. I am getting Success = false in my application. in POSTMAN, no headers and…
Mangrio
  • 1,000
  • 19
  • 41
4
votes
0 answers

How to manually handle a 302 response with angular?

I am building an angular app and we are making a get http request with the http module. The response is 302, and is being automatically redirected to a different url. Our problem is that we need to catch this 302 and stop this second request from…
slipperypete
  • 5,358
  • 17
  • 59
  • 99