ThomasL
ThomasL

Reputation: 930

How to properly convert json array in axios query parameter?

i struggle to do something that look simple with axios :

Here is my axios query :

axios.get('/consumption_reports', {
    params: {
        exists: {
            energyDeliveryPoint : false
        }
     }
})

The result is this query :

consumption_reports?exists={"energyDeliveryPoint":false}

When i would like this result :

consumption_reports?exists[energyDeliveryPoint]=false

I tried many different solution but could not found the one that works.
I look forward any answer that keep code simple.
PS : I do not want to hard code the url with my desired behavior.

Upvotes: 0

Views: 183

Answers (2)

Dmitriy Mozgovoy
Dmitriy Mozgovoy

Reputation: 1597

It works with Axios 1.x

import axios from 'axios';

const {data} = await axios.get('http://httpbin.org/get', {
  params: {
    exists: {
      energyDeliveryPoint : false
    }
  }
});

console.log(axios.VERSION, data.url); // 1.3.0 http://httpbin.org/get?exists[energyDeliveryPoint]=false

Upvotes: 1

ThomasL
ThomasL

Reputation: 930

I finally found myself, in that case i have no choice to do this :

axios.get('/consumption_reports', {
    params: {
        "exists[energyDeliveryPoint]": false
    }
})

Upvotes: 0

Related Questions