Home > Software design >  Query params seperator differs in Java and Javascript for semicolon
Query params seperator differs in Java and Javascript for semicolon

Time:09-24

For such url example.com?head=1&foot=1;id Javascript URLSearchParams and Java URLEncodedUtils.parse returns different results.

Which one should I take into account?

Javascript :

let url = new URLSearchParams("?head=1&foot=1;id")
const params = Object.fromEntries(url.entries());
console.log(url) // --> { head: '1', foot: '1;id' }

Java:

 List<NameValuePair> params = URLEncodedUtils.parse("head=1&foot=1;id", StandardCharsets.UTF_8);
 System.out.println(params); // --> [head=1, foot=1, id]

CodePudding user response:

Looks like you are talking about the semi-colon part.

URLEncodedUtils you have used is now deprecated or you might be using this.

Semicolon was used to be a separator in query string, similar to what & does.

In the recommendations of 1999, W3C recommended that all web servers support semicolon separators in addition to ampersand separators to allow application/x-www-form-urlencoded query strings in URLs within HTML documents without having to entity escape ampersands.

Since 2014, W3C recommends to use only ampersand as query separator

Read about it here: https://en.wikipedia.org/wiki/Query_string#Web_forms

  • Related