Goal
I want to pass query params for a GET request using axios
. The param value is a variable of type string and has whitespace.
Problem
It seems axios
is encoding the param in a format that my backend does not understand. I have done research on axios
encoding and it appears axios
encodes whitespace to a +
and not %20
.
Example
Let's say you have this request:
const whitespace = "white space";
const encodeWhitespace = encodeURI(whitespace);
const noSpace = "no";
axios.get('/api', {
params: {
'foo': 'bar',
'bar': 'hello world',
'space': whitespace,
'encode': 'hello%20world',
'encoded': encodeWhitespace,
'simple': noSpace
}
}
The params foo, bar, encode, simple
all work and generate a response with the correct data. The params space, encoded
do not generate the correct data. The request is successful with 200 but returns no data.
I created the same request in Postman with the same queries to see if the GET
returns the expected result and it does. I added the %20
in Postman and it returns just fine. I added the +
in Postman and that returns the expected result as well.
Question
What might be going wrong with the variable implementation? I can't do it without the variable like the bar
param because the value is being passed to a function (Redux action). Any ideas or thoughts on this would be helpful. Please comment if more information is needed.