Explain Codes LogoExplain Codes Logo

What does %5B and %5D in POST requests stand for?

java
url-encoding
java-encoding
url-decoding
Anton ShumikhinbyAnton Shumikhin·Jan 5, 2025
TLDR

Symbols %5B and %5D act as URL-encoded UTF-8 characters for [ (square bracket open) and ] (square bracket close), respectively. These come into play when assuring the safe transmission of data when sending form data via POST requests. In Java, we decode them as follows:

String decoded = URLDecoder.decode("field%5B%5D", StandardCharsets.UTF_8.name()); // "Hocus Pocus" and voila, it simplifies to "field[]"

Unmasking the URL encoding

These % notations might look like gobbledygook at first glance, but they are actually hexadecimal values representing URL-encoded special characters. To ensure data transmission across the network adheres to the conventions, we use percent encoding standards defined by RFC3986, which, among other things, reserves [ and ] for IPv6 encodings. So when these brackets come around as data in your query string, we need to turn them into %5B and %5D.

When Java saves the day

Encountering encoded data like user%5Blogin%5D in a POST request? Time for Java to work its magic:

String userLoginKey = URLDecoder.decode("user%5Blogin%5D", StandardCharsets.UTF_8.name()); // Gives you a lovely "user[login]", making sense now, isn't it?

This comes in handy when crafting or parsing data structures like maps that utilize square brackets.

With the tropical storm of special characters and URL encoding, remember these areas of potential danger:

  1. Incomplete encoding: You voyage around %5 but can't find B or D. Captain, there's something missing here!
  2. Double trouble: Encoding an encoded character could be a disaster. %5B turns isn't a double rainbow but a double trouble, turning into %255B.
  3. Misinterpretation: When you see encoded form in a place that wants literal characters, abandon ship!

Avoid shipwreck: Always validate and URL constructs and decodings.

Tips for Java knights

You are a Java Knight, journeying through the land of URLs, battling special characters. Here are keys to remember:

  • URLEncoder and URLDecoder are your Excalibur: Always at your side for encoding and decoding tasks.
  • Know your enemy: Remember %xx is a hexadecimal notation. The dragons %5B and %5D are just the beasts [ and ] masked.
  • Don't confuse your tools: Though encodeURIComponent and encodeURI seems same in the kingdom of JavaScript, remember the former has a bigger arsenal and encodes more characters.

Here is a secret spell to encode a parameter in Java:

String encodedKey = URLEncoder.encode("user[key]", StandardCharsets.UTF_8.name()); // Tada! "user%5Bkey%5D", sprinkling some encoding magic dust!

Extra arrows in your quiver

Further cases you may face in your coding adventures:

  • Internationalization: Non-ASCII characters get a fancy encoding, they like to globetrot without causing a fuss.
  • Form data: AJAX is like a dance, every step, like submitting form data, must be encoded correctly or you may fall on the stage. Always follow the rhythm!