0

Can the params object of an Axios config of a get request accept a query parameter with no value, to construct a URL such as http://example.com/foo?a=true&b? Perhaps something like:

const val = true
axios.get('/api/my_page/', {
    params: {
        a: val,
        b // although JavaScript interprets this as `b: b`
    }
})

Or is my only option to put the query parameters directly in the URL, with something like:

axios.get(`/api/my_page/?a=${val}&b`)
mic
  • 1,190
  • 1
  • 17
  • 29

1 Answers1

2

If there's no purpose of having the variable b being passed, I'll suggest skipping it altogether. Else you can follow of the below ways

params: {
    a: val,
    b: null
}

This will give you the query params as /api/my_page/?a=hello

or you can use the one below

params: {
    a: val,
    b: ''
}

This will give you the query params as /api/my_page/?a=hello&b=

I don't think it's really possible to get b alone. You either get b= or nothing, unless you explicitly hard-code it as you've shown in the one below

axios.get(`/api/my_page/?a=${val}&b`)

But this will be identical to b= in behaviour.

Also, when using the object

params: {
    a: val,
    b
}

it might throw an error. At least it does on JsFiddle React 0.14.3

Debargha Roy
  • 2,320
  • 1
  • 15
  • 34