376

I'm trying to write a Java class to log in to a certain website. The data sent in the POST request to log in is

user%5Blogin%5D=username&user%5Bpassword%5D=123456

I'm curious what the %5B and %5D means in the key user login.

How do I decode these data?

Rakib Ansary
  • 4,078
  • 2
  • 18
  • 29

8 Answers8

648

As per this answer over here: str='foo%20%5B12%5D' encodes foo [12]:

%20 is space
%22 is quotes
%5B is '['
and %5D is ']'

This is called percent encoding and is used in encoding special characters in the url parameter values.

EDIT By the way as I was reading https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURI#Description, it just occurred to me why so many people make the same search. See the note on the bottom of the page:

Also note that if one wishes to follow the more recent RFC3986 for URL's, making square brackets reserved (for IPv6) and thus not encoded when forming something which could be part of a URL (such as a host), the following may help.

function fixedEncodeURI (str) {
    return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}

Hopefully this will help people sort out their problems when they stumble upon this question.

srk
  • 4,857
  • 12
  • 65
  • 109
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
49

They represent [ and ]. The encoding is called "URL encoding".

ruakh
  • 175,680
  • 26
  • 273
  • 307
19

[] is replaced by %5B%5D at URL encoding time.

Zachary Weixelbaum
  • 904
  • 1
  • 11
  • 24
Lalit Bhudiya
  • 4,332
  • 4
  • 26
  • 32
12

Well it's the usual url encoding

So they stand for [, respectively ]

Voo
  • 29,040
  • 11
  • 82
  • 156
10

To find out these values you can simply use Console in your browser and do the following

console.log(decodeURI('user%5Blogin%5D=username&user%5Bpassword%5D=123456'))
Xeriaz
  • 131
  • 1
  • 7
4

To take a quick look, you can percent-en/decode using this online tool.

Krzysztof Przygoda
  • 1,217
  • 1
  • 17
  • 24
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/19174017) – Peter Mar 20 '18 at 20:09
  • @Peter Thanks, but this is only a link to the online tool, which can be helpful here. Since the answers was already provided there is no need for further explanations. – Krzysztof Przygoda Mar 21 '18 at 11:02
2

Not least important is why these symbols occur in url. See https://www.php.net/manual/en/function.parse-str.php#76792, specifically:

parse_str('foo[]=1&foo[]=2&foo[]=3', $bar);

the above produces:

$bar = ['foo' => ['1', '2', '3'] ];

and what is THE method to separate query vars in arrays (in php, at least).

bbe
  • 344
  • 3
  • 16
1

The data would probably have been posted originally from a web form looking a bit like this (but probably much more complicated):

<form action="http://example.com" method="post">

  User login    <input name="user[login]"    /><br />
  User password <input name="user[password]" /><br />

  <input type="submit" />
</form>

If the method were "get" instead of "post", clicking the submit button would take you to a URL looking a bit like this:

http://example.com/?user%5Blogin%5D=username&user%5Bpassword%5D=123456

or:

http://example.com/?user[login]=username&user[password]=123456

The web server on the other end will likely take the user[login] and user[password] parameters, and make them into a user object with login and password fields containing those values.

mwfearnley
  • 3,303
  • 2
  • 34
  • 35