1

I want to send request like this https://pixabay.com/api/?key=1231231231231&per_page=10. But this code sends request like this https://pixabay.com/api/?key=1231231231231/per_page=10. How do I fix it? Can't get how to set query parameter

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const api = createApi({
  reducerPath: "api",
  baseQuery: fetchBaseQuery({
    baseUrl: `https://pixabay.com/api/?key=${process.env.REACT_APP_API_KEY}`,
  }),
  endpoints: builder => ({
    getPhotos: builder.query({
      query: (limit = 10) => `per_page=${limit}`,
    }),
  }),
});

export const { useGetPhotosQuery } = api;

2 Answers 2

2
query: (limit = 10) => `per_page=${limit}`,

is just short for

query: (limit = 10) => ({
  url: `per_page=${limit}`,
  method: 'GET'
}),

you probably want to do something like

query: (limit = 10) => ({
  params: { per_page: limit }
}),
Sign up to request clarification or add additional context in comments.

2 Comments

Where did the url prop in query: (limit = 10) => ({params: { per_page: limit }}), go?
in this case, it would be an empty string so no need to specify it.
0
getPhotos: builder.query({
  query: (limit = 10) => ({
    url: "",
    method: "GET",
    params: { per_page: limit },
  }),
}),

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: stackoverflow.com/help/how-to-answer . Good luck 🙂

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.