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 '...
Angular, React, JavaScript, Java, PHP, SQL, C#, Vue, NodeJs, TypeScript and Interview Questions Answers