Multiple fields with same key in query params (axios request)?

前端 未结 5 1977
甜味超标
甜味超标 2020-12-05 04:27

So the backend (not under my control) requires a query string like this:

http://example.com/?foo=5&foo=2&foo=11

But axios

5条回答
  •  青春惊慌失措
    2020-12-05 05:09

    In Axios request config, you can override params serialization and then use QS NPM module to serialize array with repeat mode

    let params = { foo: [5, 2] }
    
    axios.get('path/to/api/',{params}) // URL : https://path/to/api?foo[]=5&foo[]=2
    
    let myAxios = axios.create({
        paramsSerializer: params => Qs.stringify(params, {arrayFormat: 'repeat'})
    })
    myAxios.get('path/to/api/',{params}) // URL : https://path/to/api?foo=5&foo=2
    

提交回复
热议问题