-1
export const getCharactersAsync = createAsyncThunk('getCharactersAsync', async (data) => {
  const response = await axios.get('users', { params: { limit: data.limit } });
  return response.data;
});

this code block allows me to control limit attribute.

export const getCharactersAsync = createAsyncThunk('getCharactersAsync', async (data) => {
  const params = new FormData();
  // const params = new URLSearchParams();
  params.append('limit', data.limit);
  const response = await axios.get('users', params);
  console.log(response);
  return response.data;
});

However I cannot control limit with using params.append. I tried URLSearchParams instead of FormData but still cannot manipulate limit attribute of the response. Why they differ from each other?

EDIT: This question has missleading information. I should have mention that i am using react-native. I found that react native doesn't fully support everything the web supports. So i need to install package called react-native-url-polyfill.Here is a github issues link https://github.com/facebook/react-native/issues/23922#issuecomment-648096619

bacayo
  • 46
  • 5
  • I see that the url has the limit in the first one. The second one shows me that only base url. – bacayo Sep 24 '22 at 11:51
  • Yes i am sure. I directly copy pasted my code to here. It is not the end of the world i can use the working one. However i try to understand why the other one not working. – bacayo Sep 24 '22 at 11:56
  • I changed to ```URLSearchParams```. It still shows me the base url. – bacayo Sep 24 '22 at 12:05

1 Answers1

1

docs

params are the URL parameters to be sent with the request. Must be a plain object or a URLSearchParams object

It can't be FormData

Solution

You wanted to use { params }, not params

export const getCharactersAsync = createAsyncThunk('getCharactersAsync', async (data) => {
  const params = new URLSearchParams();
  params.append('limit', data.limit);
  const response = await axios.get('users', { params });
  console.log(response);
  return response.data;
});
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • As you can see there is a comment line on the second code block which shows that i tried to use URLSearchParams – bacayo Sep 24 '22 at 11:57
  • thank you for response. I tried your solution and it still gives me default limit number. – bacayo Sep 24 '22 at 12:11
  • Works fine for me https://codesandbox.io/s/unruffled-babbage-iuvhd9?file=/src/index.js – Konrad Sep 24 '22 at 12:11
  • 2
    Okay clearly there's something else wrong. I try to fix it and edit. Thanks again for the help ! – bacayo Sep 24 '22 at 12:13
  • 1
    Thanks for help @Kondrad Linkowski, your solution is correct. I updated my question and added possible answer. – bacayo Sep 26 '22 at 10:08