Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTK Query: Sending Authorization returns 400 Error #4462

Closed
Devashree opened this issue Jun 15, 2024 · 2 comments
Closed

RTK Query: Sending Authorization returns 400 Error #4462

Devashree opened this issue Jun 15, 2024 · 2 comments

Comments

@Devashree
Copy link

Devashree commented Jun 15, 2024

Hello I am new to RTK Query I read several articles around how to use RTK Query to send Headers. I am trying to do something similar:

const baseQuery = fetchBaseQuery({
  baseUrl: getEndpoint(),
  prepareHeaders: (headers, { getState }) => {
    const state = (getState() as RootState)
      const accessToken = state.authToken?.accessToken;
      if (!headers.has('Authorization') && accessToken) {
      headers.set('Authorization', `Bearer ${accessToken}`)
    }
    console.log('access Token',headers);
    return headers
  },
})

const baseQueryWithReauth = async (args: string | FetchArgs, api: BaseQueryApi, extraOptions: {}) => {

  const result = await baseQuery(args, api, extraOptions);
  console.log('result is',result);
  return result
}

export const workspaceApi = createApi({
  baseQuery: baseQueryWithReauth,
    endpoints: builder => ({})
})

whenever I try to append Authorization headers to the request it returns 400 Error .
I tried to print headers it returns Headers {} . But I can see in the network log Authorization Bearer is sent returning Bad Request 400 Error. Please suggest. I tried to use same request from postman with same authorization Header the request is successful.
Console Log:
emptyHeader

Http Request returns 400 Error:

consoleRequest
I tried to hardcode Authorization HEader still 400 Issue Persist.

Please note I am using rtk-query-codegen-openapi to call Bff and the class looks something like :

import { workspaceApi as api } from '../query/workspaceApi';
const injectedRtkApi = api.injectEndpoints({
  endpoints: (build) => ({
    getCandidates: build.query<GetCandidatesApiResponse, GetCandidatesApiArg>({
      query: () => ({ url: `/api/testBooking/getCandidates` }),
    }),
})
@Devashree
Copy link
Author

@phryneas pls suggest

@rwilliams3088
Copy link

rwilliams3088 commented Sep 1, 2024

You may need to pass credentials: "include" as an argument to fetchQueryBase (that's what I have on mine):

const baseQuery = fetchBaseQuery({
  baseUrl: getEndpoint(),
  credentials: "include",
  prepareHeaders: (headers, { getState }) => {
    const state = (getState() as RootState)
      const accessToken = state.authToken?.accessToken;
      if (!headers.has('Authorization') && accessToken) {
      headers.set('Authorization', `Bearer ${accessToken}`)
    }
    console.log('access Token',headers);
    return headers
  }
})

You may also need to configure CORs server-side to permit credentials as well.

@markerikson markerikson closed this as not planned Won't fix, can't repro, duplicate, stale Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants