Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

In Linux, the shell environment uses brackets to match patterns. When using curl to send HTTP requests, URLs with special characters, such as brackets, can cause problems due to the shell interpreting these characters as part of globbing patterns.

In this tutorial, we’ll look at how to allow brackets to ensure accurate requests in a URL passed to curl.

2. Using -g or –globoff

The -g flag, which is shorthand for –globoff, is a simple and effective method for handling square brackets in curl URLs. By default, curl offers a feature known as URL globbing, which interprets square brackets to generate multiple requests based on a range or set of data.

Disabling globbing guarantees that curl considers brackets as literal characters, allowing for seamless interactions with APIs, IPv6 addresses, and other URLs containing special characters. When paired with quoting or escaping, -g offers a reliable technique for dealing with complex URL structures:

$ curl -g "https://baeldung.com/linux?filter[category]=articles&filter[status]=recent"

In this example, we use the -g flag to prevent curl from interpreting [category] and [status] as patterns. We can alternatively use –globoff in place of -g, which simply makes the flag’s purpose clearer.

Additionally, this method delivers the URL exactly as specified to the server. The server correctly reads the square brackets in the query string as being parts of the parameter names and returns a list of items that match the provided filters.

Furthermore, the -g flag guarantees that curl handles the URL as intended, avoiding interference from its globbing functionality. This makes -g essential when working with APIs or URLs that contain square brackets.

3. Escaping Brackets

Another technique to handle square brackets in curl URLs is to escape them. In Linux shells, special characters such as square brackets [ ] are used to match filenames or ranges. By escaping these characters with backslashes \, the shell passes them directly to curl, thus preventing any accidental interpretations by either the shell or curl itself:

$ curl http://baeldung.com/linux?filter\[category\]=article&filter\[status\]=recent

In this example, escaping brackets instructs the shell to interpret them as regular characters rather than special ones. Without escaping, the shell may attempt to extend [category] or [status] using filename patterns in the current directory, resulting in errors or unexpected behavior. Backslashes are a reliable and straightforward way to escape brackets in curl URLs.

4. Bracket Encoding

We can also encode brackets [ ] in the URL using their percent-encoded equivalents, %5B and %5D, respectively. This approach follows URL standards, with special characters displayed in a secure, encoded way. Furthermore, encoding brackets guarantees that shell and curl treat them as part of the URL:

$ curl "https://baeldung.com/linux?filter%5Bcategory%5D=articles&filter%5Bstatus%5D=recent"

In this example, we replace the square brackets in the original query parameters filter[category] and filter[status] with %5B and %5D. This encoding strictly adheres to HTTP standards and assures that curl doesn’t misinterpret any part of the URL. Furthermore, the program sends the encoded URL directly to the server, which identifies the encoded brackets as literal characters. The server then evaluates the query string successfully and returns the desired results.

Finally, encoding brackets ensures that our HTTP requests are compatible and accurate, especially when working with complex or dynamically generated URLs. This approach is a suitable alternative to disabling globbing and escaping brackets.

5. Conclusion

Allowing brackets in curl URLs is essential for creating valid HTTP requests, especially when working with APIs or accessing resources with complicated query parameters.

In this article, we looked at ways for disabling globbing, escaping brackets, and encoding them for URL compatibility. The -g or –globoff option prevents misinterpretation, while escaping gives a shell-level solution, and encoding assures URL compliance.

By knowing these strategies, we can reliably handle URLs containing special characters in our Linux workflows, ensuring that our operations run as intended.