Node.JS: How To Use A Percent Sign In A URL Request?

Node.JS: How To Use A Percent Sign In A URL Request?

August 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Node.JS: How To Use A Percent Sign In A URL Request?

Node.JS: Using the Percent Sign in API URL Requests

As a senior developer working extensively with Node.js and various e-commerce platforms like Magento2, I often encounter issues when trying to use special characters like the percent sign (%) in API requests. This is because most modern programming languages interpret the percent symbol as a way of expressing hexadecimal values, leading to errors when used directly in URLs.

The Problem: URI Malformed Error

In your specific case, you’re attempting to search for products with a name that contains a percentage sign using Node.js:

const request_data = {
    url: 'http://.../rest/V1/products?searchCriteria[filter_groups][0][filters][0]'+
    '[field]=name&searchCriteria[filter_groups][0][filters][0][value]=%Example%'+
    '&searchCriteria[filter Groups ][0][filters][0][condition_type]=like',
    method: 'GET',
};

However, the percent sign is causing a URI malformed error when the request is made. This is because Node.js interprets the % as a hexadecimal value instead of a literal character in the URL.

The Solution: Encoding the Percent Sign

To resolve this issue, you need to encode the percent sign and any other special characters before including them in your URL string. In JavaScript, you can use the encodeURIComponent() function for this purpose:

const encodedValue = encodeURIComponent('%Example%');

The encodeURIComponent() function replaces special characters with their corresponding UTF-8 encoding sequences. For a percent sign, it will return %25, which is the hexadecimal representation of a percent symbol.

Updating Your Request

Now, let’s update your request data to include this encoded value:

const request_data = {
    url: 'http://.../rest/V1/products?searchCriteria[filter_groups][0][filters][0]'+
    '[field]=name&searchCriteria[filter_groups][0][filters][0][value]='+encodedValue+
    '&searchCriteria[filter_groups][0][filters][0][condition_type]=like',
    method: 'GET',
};

By including encodedValue in your request, you’re ensuring that the percent sign is treated as a literal character rather than a special instruction.

Best Practices for URL Encoding

Remember to always encode any user-provided or dynamic data before including it in URLs. This helps prevent potential security vulnerabilities, such as cross-site scripting (XSS) attacks and code injection.

For more detailed information on string encoding and its importance, I recommend checking out the official MDN Web Docs on URL Encoding. This resource provides a comprehensive overview of how to safely include special characters in URLs.

Conclusion

In summary, when working with Node.js and making API requests that involve special characters like the percent sign, always ensure you’re encoding these values before including them in your URL strings. Using functions like encodeURIComponent() guarantees that your request remains well-formed and avoids errors like the URI malformed issue you encountered.

Don’t hesitate to explore more of our Node.js tutorials at LaravelCompany for additional guidance on best practices in Node.js development.

Magento 2 Catalog API Errors

Our network