Next.js Discord

Discord Forum

Configure API headers, from server side to backend

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Avatar
Asiatic LionOP
I'm developing Next.js web app and there are some API calls which are made from nextjs server-side to our back-end system.
Currently, API headers of those API calls have information of nextjs serve-side information. I'd like to rewrite or add information with client-side information.
For example, I'm going to add x-forwarded-for-original header which reflects x-forwarded-for from client side. Also need to add x-user-agent header which reflects User-Agent from client side.
And rewrite referer header with referer from client-side.
Currently, I am using Axios for http client.

How can I implement this?

7 Replies

Avatar
you can directly add the headers when you are calling the fetch() API from nextjs server side
Avatar
Asiatic LionOP
As I mentioned, I am using Axios for http client.
In the codebase, those API calls are written something like ApiClient.get(url), or ApiClient.post(url, payload).
I am looking for a way to modify Axios configuration to configure those headers for server-side.
Avatar
You can directly add the headers using axios then,
here: https://axios-http.com/docs/req_config

for curiosity, why use axios?
Avatar
Asiatic LionOP
There are not small acount of API calls which are made from server-side.
And I am not willing to configure API headers one by one.
for curiosity, why use axios?
First of all, it was there when I joined the project.
Second, I believe that is the most common API client library.
Avatar
you can just make a wrapper function that would send all the necessary headers consistently
(url: string) => axios.get(url, { ... })