Skip to content
Advertisement

URLSearchParams with axios instance in react-native

I was attempting to send payload, and URLSearchParams through an API URL from my react-native application. I initialized an axios instance as,

JavaScript

And from another component, I imported the axios instance, then,

JavaScript

The payload contains the request body for the API. As stated in API docs, I have to send the filters through URLSearchParams, like,

JavaScript

Any suggestions on how to send the URLSearchParams without hardcoding or just appending search queries to the API_URL? TIA.

Advertisement

Answer

Use a new URL() and assign the URLSearchParams() to the search property of the URL object.

You can then pass that object directly to axios

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement