URL and Query String Handling
Learn how to parse, manipulate, and encode URLs and query strings effectively. This guide covers URL structure, parameter handling, encoding best practices, and common pitfalls to avoid.
Overview
URLs are the foundation of web communication. Understanding how to properly parse, construct, and encode URLs is essential for web development, API integration, and data processing. This guide covers the complete workflow from basic URL parsing to advanced query string manipulation.
Estimated time: 20-30 minutes
Prerequisites: Basic understanding of HTTP and web URLs
Step 1: Understanding URL Structure
URLs consist of multiple components, each serving a specific purpose. Understanding these components is crucial for proper URL manipulation.
URL Anatomy
https://api.example.com:443/users/search?q=john&role=admin&page=2#results
┌─────────┬──────────────────┬────┬──────────────┬────────────────────────────┬─────────┐
│ Protocol│ Host │Port│ Path │ Query String │Fragment │
└─────────┴──────────────────┴────┴──────────────┴────────────────────────────┴─────────┘
https:// api.example.com :443 /users/search ?q=john&role=admin&page=2 #results
Components:
- Protocol: https (secure HTTP)
- Host: api.example.com (domain name)
- Port: 443 (default for HTTPS, often omitted)
- Path: /users/search (resource location)
- Query: q=john&role=admin&page=2 (parameters)
- Fragment: #results (page anchor, not sent to server) Use the URL Parser to break down URLs into their components. It extracts protocol, host, port, path, query parameters, and fragments for easy inspection.
Step 2: Parse Query Strings
Query strings encode parameters as key-value pairs in the URL. Proper parsing handles special characters, arrays, and nested structures.
Query String Examples
// Simple parameters
?name=John&age=30&city=Boston
// Array parameters (multiple values for same key)
?tags=javascript&tags=typescript&tags=react
// or
?tags[]=javascript&tags[]=typescript&tags[]=react
// Special characters (URL encoded)
?search=hello%20world&email=user%40example.com
// Empty values
?filter=&sort=name&order=asc
// Boolean-like values
?active=true&verified=false&premium=1 Use the Query String Parser to extract parameters from URLs. It handles array parameters, URL decoding, and empty values correctly.
Step 3: Build Query Strings
Construct query strings programmatically from parameter objects. Proper encoding prevents syntax errors and security issues.
Building Query Strings
// Input parameters
{
"search": "hello world",
"category": "books & media",
"price_min": 10,
"price_max": 50,
"tags": ["fiction", "bestseller"]
}
// Output query string (properly encoded)
?search=hello%20world&category=books%20%26%20media&price_min=10&price_max=50&tags=fiction&tags=bestseller
// Common encoding:
// Space → %20 or +
// & → %26
// = → %3D
// ? → %3F
// # → %23
// / → %2F Use the Query String Parser to build query strings from parameter objects. It automatically handles URL encoding and array parameters.
Step 4: URL Encoding and Decoding
URL encoding converts special characters to percent-encoded format (%XX) for safe transmission. Always encode user input before adding it to URLs.
Characters Requiring Encoding
// Reserved characters (have special meaning in URLs)
: / ? # [ ] @ ! $ & ' ( ) * + , ; =
// Unsafe characters (can cause parsing issues)
Space " < > % { } | \ ^ ` ~
// Examples:
Original: hello world
Encoded: hello%20world
Original: user@example.com
Encoded: user%40example.com
Original: 50% discount
Encoded: 50%25%20discount
Original: path/to/file
Encoded: path%2Fto%2Ffile Use URL Encode to convert text to URL-safe format and URL Decode to convert back to readable text.
Encoding Best Practices
- Always encode user input: Prevent injection attacks and parsing errors
- Encode query values, not keys: Parameter names rarely need encoding
- Use proper encoding context: Query strings vs path segments have different rules
- Don't double-encode: Check if data is already encoded before encoding again
- Decode before display: Show human-readable text to users
Step 5: Handle Special Cases
Real-world URLs often contain edge cases that require special handling. Understanding these scenarios prevents bugs and security issues.
Common Edge Cases
// 1. Multiple values for same parameter
?color=red&color=blue&color=green
// Parse as array: ["red", "blue", "green"]
// 2. Empty parameter values
?filter=&sort=name
// filter: "" (empty string)
// sort: "name"
// 3. Parameters without values
?debug&verbose&trace
// All evaluate to true or empty string
// 4. Plus sign ambiguity
?search=hello+world
// Could mean: "hello world" or "hello+world"
// Modern: Use %20 for spaces, + for literal plus
// 5. Fragment with query-like syntax
#section?param=value
// Fragment, not query parameter
// 6. International characters
?city=São%20Paulo&name=François
// Requires UTF-8 encoding Step 6: Validate and Sanitize URLs
Always validate URLs from untrusted sources to prevent security vulnerabilities and application errors.
URL Validation Checklist
- ✓ Check protocol (http, https, or allowed custom protocols)
- ✓ Validate domain format (no spaces, valid TLD)
- ✓ Verify port number (1-65535 if specified)
- ✓ Sanitize path (prevent directory traversal)
- ✓ Validate query parameters (expected keys and value types)
- ✓ Check for malicious patterns (javascript:, data:, file:)
- ✓ Limit URL length (browsers have ~2000 character limits)
- ✓ Encode special characters properly
Complete Workflow Example
Here's how all the tools work together for URL and query string handling:
- Receive URL: Get URL from user input, API, or configuration
- Parse URL: Extract components with URL Parser
- Decode Values: Convert encoded text with URL Decode
- Parse Query: Extract parameters with Query String Parser
- Modify Parameters: Add, update, or remove query parameters
- Encode Values: Convert special characters with URL Encode
- Build Query: Construct new query string with Query String Parser
- Reconstruct URL: Combine components back into complete URL
Best Practices
- Use HTTPS: Always use secure protocols for sensitive data
- Keep URLs short: Long URLs are harder to share and may be truncated
- Use meaningful parameter names: Make URLs self-documenting
- Encode properly: Use URL encoding for query values, not path segments
- Avoid sensitive data in URLs: URLs are logged and visible in browser history
- Use POST for mutations: GET requests with query strings should be idempotent
- Validate all inputs: Never trust user-provided URLs or parameters
- Handle arrays consistently: Choose one format (tags[] or multiple tags) and stick to it
- Document parameter formats: Specify expected types and formats in API docs
- Test edge cases: Empty values, special characters, very long URLs
Common Use Cases
Search and Filtering
Build search URLs with filters: ?q=laptop&category=electronics&price_max=1000&sort=price
Pagination
Add page and limit parameters: ?page=2&limit=20&sort=created_at&order=desc
Tracking and Analytics
Include UTM parameters: ?utm_source=email&utm_medium=newsletter&utm_campaign=spring_sale
API Requests
Construct API URLs with filters and includes: ?fields=id,name,email&include=profile,settings
Deep Linking
Create shareable URLs with state: ?tab=settings§ion=privacy&highlight=cookies
Related Tools
- URL Parser - Parse URLs into components
- Query String Parser - Parse and build query strings
- URL Encode - Encode text for URLs
- URL Decode - Decode URL-encoded text
- Base64 Encode - Encode binary data
- Base64 Decode - Decode base64 data
- HTML Encode/Decode - Handle HTML entities
- JSON Formatter - Format API responses
Frequently Asked Questions
What's the difference between URL encoding and Base64 encoding?
URL encoding (percent encoding) converts special characters to %XX format for safe use in URLs. Base64 encoding converts binary data to ASCII text. Use URL encoding for query parameters, Base64 for encoding binary data or when you need to embed data in URLs without special characters.
Should I use + or %20 for spaces in URLs?
Modern practice: use %20 for spaces in all parts of URLs. The + character is ambiguous (could mean space or literal plus). Some older systems treat + as space in query strings, but %20 works everywhere and is unambiguous.
How do I handle array parameters in query strings?
Two common formats: 1) Repeat the parameter: ?tags=js&tags=ts&tags=react, or 2) Use brackets: ?tags[]=js&tags[]=ts&tags[]=react. Choose one format and use it consistently. Document your choice in API documentation.
Can I put sensitive data in query strings?
No! Query strings appear in browser history, server logs, and referrer headers. Never put passwords, tokens, or personal data in URLs. Use POST requests with body data or secure headers for sensitive information.
What's the maximum length for a URL?
No official limit, but practical limits exist: browsers typically support ~2000 characters, servers vary (Apache: 8KB, Nginx: 4-8KB). Keep URLs under 2000 characters for compatibility. Use POST requests for large amounts of data.
How do I handle international characters in URLs?
Use UTF-8 encoding and percent-encode non-ASCII characters. For example, "São Paulo" becomes "S%C3%A3o%20Paulo". Modern browsers handle this automatically, but always encode programmatically to ensure compatibility.