Add HTTP Header fields to Composition API Client

Hello,

how can we add additional HTTP Header fields to the requests of the Composition APIs imx-api Client? Currently we are using the Client instance that is provided by the imx_SessionSerivce.

Thanks in advance.

  • Hello Daniel,

    While there is no support to configure this directly, you can easily create a Client instance that uses a custom function to create HTTP requests. This is the second constructor parameter of the Client class.

    (By default, the Client class uses the window.fetch method.)

    So you can use this code as a starting point to wrap the window.fetch method in your own handler.


        const myfetch = {
          fetch: (requestInfo, options)=> {
            // TODO: patch options to include additional headers here
            return window.fetch(requestInfo, options);
          }
        }
    
        this.Client = new Client(baseUrl, myfetch);
    

  • Thank you Hanno,

    that was exactly what i needed as i was uncertain how i can return a proper Promise<Response> for the fetch function.