From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Assignment: Modifying request headers

Assignment: Modifying request headers

Let's implement one more assignment where we modify the request headers with the help of interceptors. Consider a scenario where the NestJS app interacts with an external API that requires JSON formatted requests only. So we are going to convert the outgoing request to the JSON format using the interceptor. We already have the interceptor file ready, I will create a constant named request and assign the context dot switch to HTTP dot get request. With this, we would be able to access the request object. In the next step, we will specify the content type headers to ensure the request is in JSON format. To do this, we will set the content type header of the request to application slash JSON. So with this line of code, request headers are modified, and the content type is set to application slash JSON for all the outgoing requests. This tells the server that the data being sent is in JSON format. Let's also log the headers. I'll give console log and display the headers. Now in the…

Contents