Skip to main content

Posts

Showing posts with the label HTTP vs HttpClient

How to set a custom header on the request?

How to set a custom header on the request? To set a custom header on the request, firstly we need to instantiate HttpHeaders() object and pass ('header',   'value') into a function. let headers = new HttpHeaders (). set ( 'Content-Type' , 'text' ); In the above example we set “Content-Type” header value to be “text” and the default header “Content-Type” is – “application/json” It is of type immutable Map so if you assign a new value it will reinitialize the object. let requestHeaders = new HttpHeaders (). set ( 'Content-Type' , 'application/json' ); requestHeaders = requestHeaders . set ( 'authorization' , 'Bearer ' + token ); We can also append headers by chaining HttpHeaders() constructor and will look like this- let requestheaders = new HttpHeaders (). set ( 'Content-Type' , 'application/json' )                     . set ( 'authorization' , 'Bearer '...

What Are Angular HttpHeaders?

What Are HttpHeaders? The Http Headers is immutable Map and each and every set() returns a new instance and applies the changes with lazy parsing. An immutable set of Http headers, with lazy parsing. HttpHeaders Constructor - constructor ( headers ?: string | { [name: string]: string | string [];}); Imports HttpHeaders from - import { HttpHeaders } from '@angular/common/http' ; HttpHeaders class contains the list of methods - 1.       has() - Checks for existence of header by given name. 2.       get() - Returns the first header that matches given name. 3.       keys() - Returns the names of the headers 4.       getAll() - Returns list of header values for a given name. 5.       append() - Append headers by chaining. 6.       set() -   To set a custom header on the request for a given name ...

What’s the difference between HTTP and HttpClient?

Angular HTTP vs. HttpClient - The HttpClient is used to perform HTTP requests and it imported form @angular/common/http. The HttpClient is more modern and easy to use the alternative of HTTP. HttpClient is an improved replacement for Http. They expect to deprecate Http in Angular 5 and remove it in a later version. It's an upgraded version of http from @angular/http module with the following improvements – 1.       Immutable request and response objects 2.       Interceptors allow middleware logic to be inserted into the pipeline 3.       Progress events for both request and response 4.       Typed 5.       Event firing 6.       Synchronous response body access 7.       Support JSON body types and JSON by default and now, no need to be explicitly parsed 8.     ...