简体   繁体   中英

XHR request with literal URL

I'm trying to request an somewhat buggy API that is requiring some characters to be URL encoded (in my case, the dot . is causing the issue)

GET /projects/:projectid/merge_requests

With project_id being groupname/project.name that the API needs urlencoded ( including the dot ), otherwise it does respond with a 404

The correct URL is /projects/groupname%2Fproject%2Ename/merge_requests

I'm currently doing the replacement manually but it seems the browser thinks I don't know what I'm doing an replaces %2E with . in the actual request. And so I can't query my API

Current code looks like:

var projectid = 'foo/bar.baz';

projectid = encodeURIComponent(projectid).replace(/[.]/g, '%2E');

var url = 'http://gitlab.local/api/v3/projects/' + projectid + '/merge_requests'

var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.responseType = 'json';
xhr.addEventListener('load', mycallback);
xhr.send();

// -> GET http://gitlab.local/api/v3/projects/foo%2Fbar.baz/merge_requests 404 ()

Is there any way to force the URL to be sent literally and not transformed by the browser?

(I have the same behavior in Chrome and Firefox)

Instead of %2E , replace it with %252E . %2E is the escape code for . , so it will interpret it as a period. What you need is to escape the % , and that is what %25 does. When unescaped, %252E becomes %2E .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM