angular-httpclient

Angular - HttpClient: Map Get method object result to array property

二次信任 提交于 2019-12-03 02:22:45
I am calling an API that returns a JSON Object. I need just the value of the array to map to a Observable . If I call api that just returns the array my service call works. Below is my sample code .. // my service call .. import { Injectable } from '@angular/core'; import {Observable} from 'rxjs/Observable'; import {Show} from '../models/show'; import {HttpClient} from '@angular/common/http'; @Injectable() export class MyService { constructor(private http: HttpClient ) { } findAllShows(): Observable<Show[]> { return this.http .get<Show[]>(`${someURL}/shows`) } } If the return is a JSON Object

How to mock Angular 4.3 httpClient an error response in testing

空扰寡人 提交于 2019-12-03 00:01:43
I have a below interceptor auth-interceptor.service.ts import {Injectable, Injector} from '@angular/core'; import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http'; import {Observable} from 'rxjs/Observable'; import {Cookie} from './cookie.service'; import {Router} from '@angular/router'; import {UserService} from './user.service'; import {ToasterService} from '../toaster/toaster.service'; import 'rxjs/add/operator/catch'; import 'rxjs/add/observable/throw'; @Injectable() export class AuthInterceptor implements HttpInterceptor { constructor

POST request to a local json file using HttpClient

喜欢而已 提交于 2019-12-02 12:03:54
问题 I have one json file named fake.json inside assets in my angular application. Path of this file is like this. MyApp => src => assets => json => fake.json I want to make a POST request to this file using HttpClient in my component which is in inside app folder. MyApp => src => app => Statistics => statistics.component.ts Component source code export class StatisticsComponent { persons: Person[]; options = { sDom: 'rt<"bottom"p>', pagingType: 'full_numbers', pageLength: 10, serverSide: true,

map json with AngularHttpClient

老子叫甜甜 提交于 2019-12-02 11:29:07
After updating my project to use the HttpClient module instead of the Http module, the following no longer works. The problem is Property json does not exist on type object . I do need to get the items property. How can I achieve this? private loadLatestVideosForChannelId( channelId: string ): Promise<any[]> { // load videos from youtube-data-api let videos = this.http.get( 'https://www.googleapis.com/youtube/v3/search' + '?key=' + this.apiKey + '&channelId=' + channelId + '&part=snippet,id' + '&order=date' + '&type=video' + '&maxResults=3' ) .pipe( // if success map( res => { return res.json(

Why is Angular not choosing correct overload for HttpClient.get(…)?

戏子无情 提交于 2019-12-02 10:54:48
I'm trying to abstract out some HttpClient calls (.get(...) and .post(...)) into a BaseService class since I'm having to duplicate code such as headers and credentials if my services don't inherit from a base class. In doing this, for some reason my code isn't choosing the generic overload on the get. The following method call is successfully choosing the correct HttpClient.get(...) overload: However, the following is choosing a different overload, and I have no idea how to fix it: Is this because I'm declaring the private options field incorrectly? I can't find in the API docs a better

StaticInjectorError[HttpClent]: Function/class not supported

随声附和 提交于 2019-12-02 02:41:06
I am trying to inject the HttpClientModule manually which runs outside independent(may be!) from app. Before static injector i was using reflective injector and code was working fine but now that reflective injector has been deprecated and I want to update my code with static injector. //appInjector.ts export class AppInjector { private static _instance: AppInjector = new AppInjector(); private _injector; constructor() { console.log('app-injector'); AppInjector._instance = this; this._injector = ReflectiveInjector.resolveAndCreate([ ...[getAnnotations(HttpClientModule)[0].providers], MY_HTTP

405 Method Not Allowed despite CORS

孤街醉人 提交于 2019-12-01 20:41:00
I am trying to develop a frontend application using Angular. Since I added the authorization header to the HTTP POST and GET requests, I'm getting 405 Method Not Allowed , although I seemingly allow everything on the server side. The debugger in my browser Chrome says it's asking for Access-Control-Request-Method: POST and Access-Control-Request-Headers: authorization , my backend allows both, access-control-allow-methods: GET, POST and access-control-allow-headers: authorization , as well as access-control-allow-credentials: true . I don't see what I'm missing here. The server is a node.js

Getting response headers from HttpClient post request in Angular?

不想你离开。 提交于 2019-12-01 20:16:45
I'm trying to get the response headers from a post request, but the HttpResponse object doesn't contain the same headers that I can see in the network. What am I doing wrong? I need to access the value of the Apiproxy-Session-Id key and it isn't present in the HttpHeaders. This is my code to execute the post request and log the full response, where http is an HttpClient object. this.http.post('http://localhost:8081/user/login', JSON.stringify(requestBody), {observe: 'response'}).subscribe(resp => { console.log(resp); }); This is the response I'm logging. These are the headers I'm seeing in the

Angular 4 How to return multiple observables in resolver

你说的曾经没有我的故事 提交于 2019-12-01 19:52:20
Basically as a title states, I need to return multiple observables or maybe a results. The goal is basically to load lets say a library list and then load a books based on that library ID's. I don't want to call a service in components, instead I want all the data to be loaded before the page load. import { Injectable } from '@angular/core'; import { Resolve, ActivatedRouteSnapshot } from '@angular/router'; import { UserService } from './../_services/index'; @Injectable() export class LibraryResolver implements Resolve<any> { constructor(private _userService: UserService) {} resolve(route:

Angular 4 How to return multiple observables in resolver

元气小坏坏 提交于 2019-12-01 17:34:22
问题 Basically as a title states, I need to return multiple observables or maybe a results. The goal is basically to load lets say a library list and then load a books based on that library ID's. I don't want to call a service in components, instead I want all the data to be loaded before the page load. import { Injectable } from '@angular/core'; import { Resolve, ActivatedRouteSnapshot } from '@angular/router'; import { UserService } from './../_services/index'; @Injectable() export class