1

I am trying to request a simple API search from Magento2, but apparently I cannot use the % sign inside the URL request. Is there another way to use the percentage sign inside the URL?

  • The %_% is after [value]

Request Link:

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',
};

Console Error:

URIError: URI malformed

at decodeURIComponent (<anonymous>)
at OAuth.deParam (/Users/Kevin/Desktop/TestingModules/node_modules/oauth-1.0a/oauth-1.0a.js:227:27)
at OAuth.deParamUrl (/Users/Kevin/Desktop/TestingModules/node_modules/oauth-1.0a/oauth-1.0a.js:245:17)
at OAuth.getParameterString (/Users/Kevin/Desktop/TestingModules/node_modules/oauth-1.0a/oauth-1.0a.js:142:132)
at OAuth.getBaseString (/Users/Kevin/Desktop/TestingModules/node_modules/oauth-1.0a/oauth-1.0a.js:124:130)
at OAuth.getSignature (/Users/Kevin/Desktop/TestingModules/node_modules/oauth-1.0a/oauth-1.0a.js:100:36)
at OAuth.authorize (/Users/Kevin/Desktop/TestingModules/node_modules/oauth-1.0a/oauth-1.0a.js:87:39)
at Object.module.exports.init (/Users/Kevin/Desktop/TestingModules/test.js:36:43)
at [eval]:1:19
at ContextifyScript.Script.runInThisContext (vm.js:50:33)
Community
  • 1
  • 1
John C
  • 517
  • 2
  • 6
  • 16
  • Check **Ilia Rostovtsev's** answer in this thread => https://stackoverflow.com/questions/7449588/why-does-decodeuricomponent-lock-up-my-browser – David R Jul 15 '18 at 08:29

1 Answers1

5

The Issue

The % character is escape character for urls and is the first character for url-encoding special characters in urls—it denotes the beginning of an encoded character (composed of a % followed by two hexadecimal characters). The reason you are getting the URIError is because when the uri is parsed, the machine sees the % and then looks at the next two characters to tell what character is being encoded. So %Ex is mistaken for the encoding of a character (but x is not a hexadecimal character so you get a malformed URI). See https://en.wikipedia.org/wiki/Percent-encoding or https://www.ietf.org/rfc/rfc1738.txt for more.

The Solution

To encode a percent sign, replace the % with %25, which is the encoding for the percent sign.

Henry Woody
  • 14,024
  • 7
  • 39
  • 56