Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If paramsSerializer is not specified, brackets are not encoded #3316

Open
HealGaren opened this issue Oct 6, 2020 · 20 comments · May be fixed by #5715
Open

If paramsSerializer is not specified, brackets are not encoded #3316

HealGaren opened this issue Oct 6, 2020 · 20 comments · May be fixed by #5715

Comments

@HealGaren
Copy link

HealGaren commented Oct 6, 2020

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

@rkovalov
Copy link

rkovalov commented Oct 21, 2020

This happened due to library returns brackets after encoding

replace(/%5B/gi, '[').

I dont know what reason to do it

@HealGaren
Copy link
Author

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.

@stevebor1
Copy link

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.

Running Tomcat 8.0.44 was fine, but after upgrading to 8.0.59, Tomcat now enforces those to be encoded.
https://tomcat.apache.org/tomcat-8.5-doc/config/http.html
Setting relaxedQueryChars and specifying the characters to ignore works, but it would be nice if Axios can update the parameter encoding logic and encode the characters if not an array.

@stevebor1
Copy link

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

@HealGaren
Copy link
Author

HealGaren commented Jun 7, 2021

@jasonsaayman

#3477
This is a PR for the v1.0.0-beta version of this encoding issue.

No feedback since I created the PR, so I'm commenting the link.
(I'm worried about missing this, I'm not urging you to do this!)

@payros
Copy link

payros commented Jul 15, 2021

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
}

@aLx450
Copy link

aLx450 commented Sep 30, 2021

Would greatly appreciate a merge of this PR..... :)

@jasonsaayman
Copy link
Member

Please try with the latest version, should this still be the case let me know 😄

@MelMelsy
Copy link

MelMelsy commented Jul 5, 2022

Hi, tried with the latest version [v0.27.2] however, the square brackets are still not encoded.

@jasonsaayman
Copy link
Member

Please try with the pre-release v1

@marc-weber1
Copy link

marc-weber1 commented Aug 15, 2022

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.

@andrijaantunovic
Copy link

andrijaantunovic commented Feb 17, 2023

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.

@andrijaantunovic
Copy link

@jasonsaayman Seeing as this issue has been closed for a while, I'm not sure what the intended default behavior is.

But just to confirm, axios itself still does not encode brackets as of v1.3.3.

@HealGaren
Copy link
Author

HealGaren commented Feb 21, 2023

@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.

@ferhansolo
Copy link

Here to report the same issue with version 1.3.4

@jasonsaayman jasonsaayman reopened this Apr 25, 2023
@jasonsaayman
Copy link
Member

Reopened

@rhkd6351
Copy link

rhkd6351 commented Nov 16, 2023

@HealGaren
I would like to know why #5715 was not merged.
still not working

@chigia001
Copy link

This change to the default paramsSerializer can be consider breaking change for multiple people.

@shikeiyan
Copy link

This issue still not resolved using v1.6.2.

@adrlen
Copy link

adrlen commented Mar 29, 2024

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

Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet