Return to site

ANGULAR INTERVIEW QUESTION: How to define headers for all requests sent by Angular?

· ngInterview,fullstack

Angular has an HTTP 'interceptor' mechanism.

An HTTP interceptor allows you to catch all the HTTP responses going through your app, and to apply a behavior on it.

This is useful when you want to add the same header to all your output requests or when you want to intercept the server responses in error.

A classical use-case is to add an authorization header to all secure request to your application, or to add the header 'Content-Type" : 'application/json'.

 

𝐒𝐨, 𝐢𝐧𝐭𝐞𝐫𝐜𝐞𝐩𝐭𝐨𝐫𝐬 𝐢𝐬 𝐚 𝐦𝐞𝐜𝐡𝐚𝐧𝐢𝐬𝐦 𝐝𝐢𝐫𝐞𝐜𝐭𝐥𝐲 𝐢𝐧𝐭𝐞𝐠𝐫𝐚𝐭𝐞𝐝 𝐢𝐧 𝐀𝐧𝐠𝐮𝐥𝐚𝐫 𝐬𝐢𝐧𝐜𝐞 𝐢𝐭𝐬 𝐯𝐞𝐫𝐬𝐢𝐨𝐧 𝟒.𝟑.

𝐈𝐭 𝐚𝐥𝐥𝐨𝐰𝐬 𝐲𝐨𝐮 𝐭𝐨 𝐢𝐧𝐭𝐞𝐫𝐜𝐞𝐩𝐭 𝐚𝐥𝐥 𝐭𝐡𝐞 𝐇𝐓𝐓𝐏 𝐫𝐞𝐪𝐮𝐞𝐬𝐭𝐬 𝐭𝐡𝐚𝐭 𝐠𝐨 𝐭𝐡𝐫𝐨𝐮𝐠𝐡 𝐲𝐨𝐮𝐫 𝐚𝐩𝐩, 𝐚𝐧𝐝 𝐭𝐨 𝐚𝐩𝐩𝐥𝐲 𝐨𝐧 𝐢𝐭 𝐭𝐡𝐞 𝐜𝐮𝐬𝐭𝐨𝐦 𝐬𝐩𝐞𝐜𝐢𝐟𝐢𝐜 𝐛𝐞𝐡𝐚𝐯𝐢𝐨𝐫 𝐲𝐨𝐮 𝐝𝐞𝐬𝐢𝐫𝐞.