How to add multiple headers in Angular 5 HttpInterceptor

Since the set method returns headers object every time, you can do this. This way, original headers from the intercepted request will also be retained. const authReq = req.clone({ headers: req.headers.set(‘Content-Type’, ‘application/json’) .set(‘header2’, ‘header 2 value’) .set(‘header3’, ‘header 3 value’) });

How to mock Angular 4.3 httpClient an error response in testing

The expectOne method in HttpTestingController class returns a TestRequest object. This TestRequest class has a flush method which can be used to deliver both successful and unsuccessful responses. We can resolve the request by returning a body along with some additional response headers (if any). Relevant info can be found here. Now, coming back to … Read more

Angular 2: How to access an HTTP response body?

Both Request and Response extend Body. To get the contents, use the text() method. this.http.request(‘http://thecatapi.com/api/images/get?format=html&results_per_page=10’) .subscribe(response => console.log(response.text())) That API was deprecated in Angular 5. The new HttpResponse<T> class instead has a .body() method. With a {responseType: ‘text’} that should return a String.

Angular 4 Error: No provider for HttpClient

In your test TestBed.configureTestingModule({ providers: [FlexSearchService, HttpClientModule] }); It should be TestBed.configureTestingModule({ imports: [HttpClientModule], providers: [FlexSearchService] }); or even better (if you want to mock request): TestBed.configureTestingModule({ imports: [HttpClientTestingModule], providers: [FlexSearchService] });

Angular: How to download a file from HttpClient?

Blobs are returned with file type from backend. The following function will accept any file type and popup download window: downloadFile(route: string, filename: string = null): void{ const baseUrl=”http://myserver/index.php/api”; const token = ‘my JWT’; const headers = new HttpHeaders().set(‘authorization’,’Bearer ‘+token); this.http.get(baseUrl + route,{headers, responseType: ‘blob’ as ‘json’}).subscribe( (response: any) =>{ let dataType = response.type; let … Read more

How do I set the baseUrl for Angular HttpClient?

Use the new HttpClient Interceptor. Create a proper injectable that implements HttpInterceptor: import {Injectable} from ‘@angular/core’; import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from ‘@angular/common/http’; import {Observable} from ‘rxjs/Observable’; @Injectable() export class APIInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const apiReq = req.clone({ url: `your-api-url/${req.url}` }); return next.handle(apiReq); } } The HttpInterceptor can clone … Read more

Angular 4.3 – HttpClient set params

HttpParams is intended to be immutable. The set and append methods don’t modify the existing instance. Instead they return new instances, with the changes applied. let params = new HttpParams().set(‘aaa’, ‘A’); // now it has aaa params = params.set(‘bbb’, ‘B’); // now it has both This approach works well with method chaining: const params = … Read more

Angular HttpClient “Http failure during parsing”

You can specify that the data to be returned is not JSON using responseType. In your example, you can use a responseType string value of text: return this.http.post( ‘http://10.0.1.19/login’, {email, password}, {responseType: ‘text’}) The full list of options for responseType is: json (the default) text arraybuffer blob See the docs for more information.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)