Reputation: 753
We have a basic RTK API Slice that based on fetchBaseQuery just like recommended on RTK documentation.
Also, there is a backend service that returns data as a text in a response, but declared as a Content-Type: application/json
on Response Header. I know this is not a consistent but unfortunately, we don't have a privilege to change this now.
We tried to parse it in a proper way but we can't have any clue about the data, we didn't see in RTK, even cannot reached inside of transformResponse
. We have saw the expected response when we inspected the XHR/Fetch activity on the network tab and that is the only clue we received the data successfully.
I think RTK doesn't process this data because it is declared as a json on response header but provided as a text, string inside of the body. It is a little bit awkward because we don't see any error anywhere.
Do you have any idea how we can reach the response inside of the RTK?
Upvotes: 4
Views: 4227
Reputation: 49361
From docs:
By default, fetchBaseQuery assumes that every Response you get will be parsed as json. In the event that you don't want that to happen, you can specify an alternative response handler like text, or take complete control and use a custom function that accepts the raw Response object —
Example from docs:
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
export const customApi = createApi({
baseQuery: fetchBaseQuery({ baseUrl: '/api/' }),
endpoints: (builder) => ({
getUsers: builder.query({
query: () => ({
url: `users`,
// *********************************8
// this is how you handle
responseHandler: (response) => response.text(), // This is the same as passing 'text'
}),
}),
}),
})
Upvotes: 5