简体   繁体   中英

AngularJS - $http request special characters issue

I hope you are fine guys. I am trying to consume an API written by my colleague with the use of AngularJS $http. It is a PATCH method type. One of the parameters is passed in the URL (SomeMethod/User/FHD&TE) and the other one in the body (JSON). The one in the body is obviously fine, but there is an issue with the one that is passed in the URL, due to some special characters coming up in there from time to time. The API response code is 400 and it says "A potentially dangerous Request.Path value was detected from the client". I tried to use escape and encoding JS functions but none of them worked. What is more, there is an option params in the angular.http that I use to make the requests. As far as I know it takes care of encoding the parameters (solves the special characters issue), but I cannot use it, cause the URL would be different from the one that API expects. It would be "SomeMethod/User?FHDTE%" instead of "SomeMethod/User/FHD&TE" and obviously reply with 404. Do you now if this can be solved on the client side and if yes how can I do it? Or does it need an update of the backend?

Sample Request:

return $http({
                method: 'PATCH',
                url: ...SomeMethod/User/firstURLParameter,
                headers: { 'Content-Type': 'application/json' },
                data: { "secondBodyParameter": true }
            });

If server side is written in Nodejs try using unescape function for your recieved parameters.

var str = Need%20tips%3F%20Visit%20W3Schools%21; var str_1 = unescape(str)

where str1 result will be "Need tips? Visit W3Schools!"

Examples from w3schools https://www.w3schools.com/jsref/jsref_unescape.asp

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