axios: If paramsSerializer is not specified, brackets are not encoded

Describe the issue

The default behavior of paramsSerializer does not encode square brackets. There cannot be brackets in place of the url parameter, and it should be encoded.

Looking at this pull request or this comment, This behavior seems to be intentional.

But according to the RFC 3986 cited there:

A host identified by an Internet Protocol literal address, version 6 [RFC3513] or later, is distinguished by enclosing the IP literal within square brackets (“[” and “]”). This is the only place where square bracket characters are allowed in the URI syntax.

(page 19)

URL parameter is not in this case. So I think brackets should be encoded in default behavior.

Example Code

axios.get('/test', {params: {a: '[]'}})

Expected behavior, if applicable

expected : request URL is https://domain.com/test?a=%5B%5D actual: request URL is https://domain.com/test?a=[]

Environment

  • Axios Version: 0.20.0
  • Browser: Chrome
  • Browser Version: 85.0.4183.121

Additional context/Screenshots

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 14
  • Comments: 20 (3 by maintainers)

Commits related to this issue

Most upvoted comments

@andrijaantunovic A branch named 1.0.0-beta existed and the PR was merged into that branch. but, In the process of releasing the 1.0.0 stable version, it seems that all the work in the 1.0.0-beta branch was deleted. I will send a new PR when the issue is reopened.

@jasonsaayman This issue is not resolved in the latest released version(v1.3.3), so it needs to be reopened.

Regardless of server, the brackets should still be encoded by axios. What is the status of this?

Would greatly appreciate a merge of this PR… 😃

image

Starting with Tomcat 8, if the URL contains brackets, a request error is responded.

The specs of a particular program do not dictate whether this should be fixed, but I think it can be a small reason.

For anyone waiting for this fix to be incorporated into the next version of axios, here is a quick and dirty workaround using paramsSerializer and query-string (qs will also work) to strictly encode the URL:

import axios from 'axios'
import queryString from 'query-string'

// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Serialize the parameteters
    config.paramsSerializer = params => queryString.stringify(params, { arrayFormat: 'brackets' })
    return config
}

This is (no longer?) correct as paramSerializer is now an object with encode and serialize properties. Trying to override paramsSerializer directly with a callback will give you a type error.

A helpful example was provided here. I didn’t have luck with queryString lib so finally I’ve defined the serializer with qs directly when creating the axios instance, like this:

import axios from 'axios';
import qs from 'qs';

const api = axios.create({
  baseURL: 'http://localhost:3000',
  paramsSerializer: {
    serialize: (params) => {
      return qs.stringify(params, { arrayFormat: 'brackets' });
    },
  },
});

Maybe there is a cleaner way, but this seems to work fine for me.

Pre-release v1 also does not seem to work. This issue also makes it impossible to interact with the patreon API since manual URL encoding with %5B and %5D doesn’t work, since % gets encoded.

This issue still not resolved using v1.6.2.

Hello @jasonsaayman, is there any news on this issue ? Is #5715 planned to be merge soon ?

Thanks in advance

For anyone waiting for this fix to be incorporated into the next version of axios, here is a quick and dirty workaround using paramsSerializer and query-string (qs will also work) to strictly encode the URL:

import axios from 'axios'
import queryString from 'query-string'

// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Serialize the parameteters
    config.paramsSerializer = params => queryString.stringify(params, { arrayFormat: 'brackets' })
    return config
}