Try it yourself with our free Url Encoder tool — runs entirely in your browser, no signup needed.

How to URL decode in C++

How to URL decode in C++

URL decoding is the process of converting a URL-encoded string back to its original form. This is a crucial step when working with web data, as URLs often contain special characters that need to be decoded to retrieve the original data. In this article, we will explore how to URL decode in C++.

Quick Example

#include <string>
#include <curl/curl.h>

std::string urlDecode(const std::string& encodedUrl) {
    CURL *curl = curl_easy_init();
    if(curl) {
        char* decodedUrl = curl_easy_unescape(curl, encodedUrl.c_str(), encodedUrl.length(), NULL);
        if(decodedUrl) {
            std::string result(decodedUrl);
            curl_free(decodedUrl);
            return result;
        }
        curl_easy_cleanup(curl);
    }
    return "";
}

int main() {
    std::string encodedUrl = "https%3A%2F%2Fexample.com%2Fpath%3Fquery%3Dstring";
    std::string decodedUrl = urlDecode(encodedUrl);
    std::cout << decodedUrl << std::endl; // Output: https://example.com/path?query=string
    return 0;
}

To use this code, you need to install the libcurl library. On Ubuntu-based systems, you can run the following command:

sudo apt-get install libcurl4-openssl-dev

Step-by-Step Breakdown

Let's walk through the code line by line:

  1. #include <string>: We include the string header to use the std::string class.
  2. #include <curl/curl.h>: We include the curl/curl.h header to use the libcurl library.
  3. std::string urlDecode(const std::string& encodedUrl): We define a function urlDecode that takes a const std::string& as input and returns a std::string.
  4. CURL *curl = curl_easy_init();: We initialize a CURL handle using curl_easy_init().
  5. char* decodedUrl = curl_easy_unescape(curl, encodedUrl.c_str(), encodedUrl.length(), NULL);: We use curl_easy_unescape() to decode the input URL. The function takes the CURL handle, the input URL as a C-style string, the length of the input URL, and a pointer to store the decoded URL.
  6. if(decodedUrl) { ... }: We check if the decoding was successful.
  7. std::string result(decodedUrl);: We create a std::string object from the decoded URL.
  8. curl_free(decodedUrl);: We free the memory allocated by curl_easy_unescape().
  9. return result;: We return the decoded URL.

Handling Edge Cases

Empty/null input

std::string urlDecode(const std::string& encodedUrl) {
    if(encodedUrl.empty()) {
        return "";
    }
    // ...
}

In this case, we simply return an empty string if the input is empty.

Invalid input

std::string urlDecode(const std::string& encodedUrl) {
    CURL *curl = curl_easy_init();
    if(curl) {
        char* decodedUrl = curl_easy_unescape(curl, encodedUrl.c_str(), encodedUrl.length(), NULL);
        if(decodedUrl) {
            // ...
        } else {
            // Handle error
            return "";
        }
    }
    return "";
}

In this case, we check if the decoding was successful and return an empty string if it was not.

Large input

std::string urlDecode(const std::string& encodedUrl) {
    CURL *curl = curl_easy_init();
    if(curl) {
        char* decodedUrl = curl_easy_unescape(curl, encodedUrl.c_str(), encodedUrl.length(), NULL);
        if(decodedUrl) {
            std::string result(decodedUrl);
            curl_free(decodedUrl);
            // ...
        }
    }
    return "";
}

In this case, we use a std::string object to store the decoded URL, which can handle large inputs.

Unicode/special characters

std::string urlDecode(const std::string& encodedUrl) {
    CURL *curl = curl_easy_init();
    if(curl) {
        char* decodedUrl = curl_easy_unescape(curl, encodedUrl.c_str(), encodedUrl.length(), NULL);
        if(decodedUrl) {
            std::string result(decodedUrl);
            curl_free(decodedUrl);
            // Use a Unicode-aware string class, such as std::wstring
            std::wstring wideResult(result.begin(), result.end());
            // ...
        }
    }
    return "";
}

In this case, we use a Unicode-aware string class, such as std::wstring, to handle Unicode characters.

Common Mistakes

Incorrect includes

// Wrong
#include <iostream>

// Correct
#include <string>
#include <curl/curl.h>

Make sure to include the correct headers.

Incorrect function call

// Wrong
std::string decodedUrl = urlDecode(encodedUrl.c_str());

// Correct
std::string decodedUrl = urlDecode(encodedUrl);

Make sure to pass the correct type of argument to the urlDecode function.

Not freeing memory

// Wrong
char* decodedUrl = curl_easy_unescape(curl, encodedUrl.c_str(), encodedUrl.length(), NULL);
std::string result(decodedUrl);

// Correct
char* decodedUrl = curl_easy_unescape(curl, encodedUrl.c_str(), encodedUrl.length(), NULL);
std::string result(decodedUrl);
curl_free(decodedUrl);

Make sure to free the memory allocated by curl_easy_unescape().

Performance Tips

  1. Use curl_easy_unescape() instead of curl_unescape(): curl_easy_unescape() is a more efficient and safer function to use.
  2. Use a std::string object to store the decoded URL: std::string objects are more efficient than C-style strings.
  3. Avoid unnecessary copies: Avoid making unnecessary copies of the input URL or the decoded URL.

FAQ

Q: What is URL decoding?

A: URL decoding is the process of converting a URL-encoded string back to its original form.

Q: Why do I need to URL decode?

A: You need to URL decode to retrieve the original data from a URL-encoded string.

Q: What is the difference between curl_easy_unescape() and curl_unescape()?

A: curl_easy_unescape() is a more efficient and safer function to use than curl_unescape().

Q: How do I handle large inputs?

A: Use a std::string object to store the decoded URL, which can handle large inputs.

Q: How do I handle Unicode/special characters?

A: Use a Unicode-aware string class, such as std::wstring, to handle Unicode characters.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp