mst-gql icon indicating copy to clipboard operation
mst-gql copied to clipboard

How to add HTTP header to one request?

Open michal-wierzba opened this issue 2 years ago • 1 comments

How to add HTTP header to one request?

michal-wierzba avatar May 31 '23 13:05 michal-wierzba

    // Authenticate the network into the current app, save the token for future API calls.
    setAuthorizationHeader(token: string) {
      const env = getEnv(self);

      if (token) {
        // https://github.com/mobxjs/mst-gql/issues/82#issuecomment-535046885
        env.gqlHttpClient.setHeaders({authorization: token});

        env.gqlWsClient.updateOptions({
          uri: constants.wsApiUrl + `?authorization=${token}`,
          // debugIncomingMessages: true,
        });

        // https://github.com/axios/axios#custom-instance-defaults
        env.uploadClient.defaults.headers.common['Authorization'] = token;
      } else {
        env.gqlHttpClient.setHeaders({});
        env.gqlWsClient.updateOptions({
          uri: constants.wsApiUrl,
          // debugIncomingMessages: true,
        });
        delete env.uploadClient.defaults.headers.common['Authorization']
      }
    },

Here is how I did it...does that help?

Aryk avatar Jul 24 '23 02:07 Aryk