geraintluff / uri-templates

JavaScript utility for RFC 6570: URI Templates
138 stars 21 forks source link

uri encoded % #16

Closed pjlammertyn closed 9 years ago

pjlammertyn commented 9 years ago

is it correct that the uri encode representation of % is %2525 ? ex: ?filter[body_like]=%Ea aut nobis dolorem% uri encoded: ?filter[body_like]=%2525Ea%20aut%20nobis%20dolorem%2525

geraintluff commented 9 years ago

Err... no. That is not correct.

geraintluff commented 9 years ago

Could you post an example template and data, to make sure I get the issue correct?

pjlammertyn commented 9 years ago
var uriTemplates = require('uri-templates');

var uriTemplate = uriTemplates('{/type,ids,relatedField,relationship}{?query*}')
var url = 'http://localhost:8001/api/messages?filter[body_like]=animi%2525'
var params = uriTemplate.fromUri(url.split('?').map((part, index) => index > 0 ? decodeURIComponent(part) : part).join('?'))

console.log(params) //{ query: { 'filter[body_like]': 'animi%' } }

see https://github.com/fortunejs/fortune-json-api/blob/a091835c11143ad244bb3f8b0c72c2ba5348e0e7/lib/helpers.js#L33

geraintluff commented 9 years ago

Why are you percent-decoding the query string before passing it to .fromUri()?

If you skip that step, it works as expected:

var template = uriTemplates('{/type,ids,relatedField,relationship}{?query*}');
var url = 'http://localhost:8001/api/messages?filter[body_like]=animi%2525';
console.log(template.fromUri(url)); // { query: { 'filter[body_like]': 'animi%25' } }

The double-encoding error is because you're doing some munging before passing it in - what you actually end up passing in to .fromUri() is the string: "http://localhost:8001/api/messages?filter[body_like]=animi%25". The template decoder does a URI-decode for the parameters (as it should) , so the result of "animi%" is correct.

pjlammertyn commented 9 years ago

I asked, because I thought that it was a bug in fortune-json-api, but wasn't sure, even after reading the rfc6570.