Programming

How do I URL encode a string

27 September 2026 · 7 min read

How do I URL encode a string

Navigating the complex world of web development often presents subtle challenges that, if overlooked, can lead to broken links, data corruption, or even security vulnerabilities. One such critical but often misunderstood concept is URL encoding. If you’ve ever asked, “How do I URL encode a string?” then you’re on the right track to ensuring your web applications handle data robustly and securely. This process, also known as percent-encoding, is fundamental for transmitting data reliably across the internet, especially when dealing with characters that aren’t standard alphanumeric. Mastering URL encoding is not just a best practice; it’s a necessity for maintaining the integrity and functionality of your web resources, from simple query parameters to complex API requests. It ensures that every piece of information sent through a URL arrives at its destination exactly as intended, without misinterpretation by browsers or servers.

What is URL Encoding and Why is it Essential?

URL encoding is a mechanism used to translate characters that might be misinterpreted by web servers or browsers into a format that is universally understood. Specifically, it converts characters that are not part of the standard URL character set (like spaces, ampersands, or question marks) into a percent-encoded format. This ensures that every component of a Uniform Resource Locator (URL) remains valid and unambiguous, preventing parsing errors or security exploits.

The Core Problem: Special Characters

The internet relies on URLs to locate resources, but not all characters are safe to use directly within a URL. Characters such as spaces, slashes, question marks, and ampersands have special meanings within the URL structure. For instance, a space character would typically terminate a URL in a browser, while an ampersand (&) is used to separate query parameters. If these characters appear as part of data (e.g., a search term like “science & technology”), they must be converted. Without proper URL encoding, a server might interpret “science & technology” as two separate parameters: “science " and " technology”, completely altering the intended query.

According to Tim Berners-Lee’s original specification for URLs, only a specific subset of ASCII characters is permitted unencoded. All other characters, including non-ASCII characters like those from different languages (e.g., accented letters, Cyrillic, Chinese characters), must be encoded. This crucial step guarantees that URLs remain consistent and universally interpretable across diverse systems and character sets like UTF-8. Failing to encode these characters can lead to broken links, incorrect data retrieval, or even server-side errors, making the web experience unreliable.

Beyond Basic Navigation: Data Integrity and Security

The importance of URL encoding extends far beyond mere cosmetic tidiness; it is a cornerstone of data integrity and web security. When user-generated content or dynamic data is inserted into a URL, proper encoding prevents characters from being misinterpreted as part of the URL’s structure rather than its content. This safeguards against potential vulnerabilities such as Cross-Site Scripting (XSS) or SQL injection, where malicious input could be executed if not properly sanitized and encoded. Encoding ensures that characters like < or >, which could be part of an HTML tag, are treated as literal data, not executable code.

Moreover, URL encoding is vital for maintaining data consistency across different platforms and programming languages. A string encoded on a client-side JavaScript application should be decoded identically by a server-side Python script or a PHP application. This interoperability is fundamental for building robust web services and APIs that exchange information seamlessly. Any deviation in encoding or decoding can lead to data loss or corruption, undermining the reliability of your entire system. For developers, a deep understanding of this process is paramount for creating secure and functional web applications that stand the test of time.

Understanding the Mechanics: How URL Encoding Works

At its heart, URL encoding, often referred to as percent-encoding, follows a simple yet powerful rule: replace unsafe characters with a percent sign (%) followed by two hexadecimal digits representing the character’s ASCII value. This method ensures that all characters are represented in a way that is safe for transmission over the internet, adhering to the standards set by RFC 3986. This standardized approach allows any web client or server to consistently interpret the data contained within a URL, regardless of its origin or destination.

The Percent-Encoding Standard

The standard dictates that certain characters are “reserved” (e.g., ?, &, /, =) because they have special meaning within a URL structure, while others are “unreserved” (e.g., alphanumeric characters, -, _, ., ~) and can appear literally. Any reserved character, or any character outside the unreserved set, must be percent-encoded if it is intended to be treated as data rather than a structural component of the URL. For example, a space character, which is not allowed in URLs, is encoded as %20. The ampersand symbol &, typically used to separate query parameters, becomes %26 when it’s part of a parameter’s value. This transformation is crucial for maintaining the intended meaning of data.

For characters outside the basic ASCII range, such as those found in international languages, the process involves converting the character into its UTF-8 byte sequence and then encoding each byte individually. For instance, the Euro symbol (€) in UTF-8 might be represented by multiple bytes (e.g., E2 82 AC), each of which would then be percent-encoded (%E2%82%AC). This multi-byte encoding ensures that a vast array of global characters can be safely and accurately transmitted via URLs, supporting a truly global web. This systematic conversion is what allows browsers and servers to correctly interpret complex data strings without ambiguity.

Common Characters and Their Encoded Forms

Understanding which common characters require encoding and what their encoded forms are can significantly aid in debugging and developing web applications. While most programming languages handle this automatically, familiarity with the conversions is invaluable. For example, a common issue arises with the forward slash (/). If a / is part of a data string within a URL path segment, it needs to be encoded as %2F; otherwise, it might be interpreted as a path delimiter, leading to an incorrect resource location.

Here are some frequently encountered characters and their corresponding percent-encoded forms:

  • Space: %20
  • Ampersand (&): %26
  • Question Mark (?): %3F
  • Equals Sign (=): %3D
  • Forward Slash (/): %2F
  • Hash/Pound Sign (): %23
  • Plus Sign (+): %2B (Note: sometimes encoded as %20 in query strings, depending on context, though %2B is technically correct for the character itself.)

It’s important to remember that while + can sometimes be interpreted as a space in query strings (a legacy from application/<b>Question & Answer : </b><br></br><p>I have a URL string (NSString) with spaces and & characters. How do I url encode the entire string (including the & ampersand character and spaces)?</p><br></br><p>Unfortunately, stringByAddingPercentEscapesUsingEncoding doesn't always work 100%. It encodes non-URL characters but leaves the reserved characters (like slash / and ampersand &) alone. Apparently this is a <em>bug</em> that Apple is aware of, but since they have not fixed it yet, I have been using this category to url-encode a string:</p> <pre>@implementation NSString (NSString_Extended) - (NSString *)urlencode { NSMutableString *output = [NSMutableString string]; const unsigned char *source = (const unsigned char *)[self UTF8String]; int sourceLen = strlen((const char *)source); for (int i = 0; i < sourceLen; ++i) { const unsigned char thisChar = source[i]; if (thisChar == ' '){ [output appendString:@"+"]; } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || (thisChar >= 'a' && thisChar <= 'z') || (thisChar >= 'A' && thisChar <= 'Z') || (thisChar >= '0' && thisChar <= '9')) { [output appendFormat:@"%c", thisChar]; } else { [output appendFormat:@"%%%02X", thisChar]; } } return output; } </pre> <p>Used like this:</p> <pre>NSString *urlEncodedString = [@"SOME_URL_GOES_HERE" urlencode]; // Or, with an already existing string: NSString *someUrlString = @"someURL"; NSString *encodedUrlStr = [someUrlString urlencode]; </pre> <hr></hr> <p>This also works:</p> <pre>NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, (CFStringRef)unencodedString, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 ); </pre> <hr></hr> <p>Some good reading about the subject:</p> <p><a href="https://stackoverflow.com/q/3423545/836407">Objective-c iPhone percent encode a string?</a><br></br> <a href="https://stackoverflow.com/q/8086584/836407">Objective-C and Swift URL encoding</a> </p> <p><a href="http://cybersam.com/programming/proper-url-percent-encoding-in-ios" rel="noreferrer">http://cybersam.com/programming/proper-url-percent-encoding-in-ios</a><br></br> <a href="https://devforums.apple.com/message/15674#15674" rel="noreferrer">https://devforums.apple.com/message/15674#15674</a> <a href="http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/" rel="noreferrer">http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/</a> </p>