user1421161 user1421161 - 1 month ago 19
Javascript Question

AngularJS 1 Restangular GET stripping #

I am currently creating a search function in AngularJS 1. I am using Restangular in order to communicate between my JS frontend and an API.

I was wondering however, does AngularJS or Restangular in particular, strip out certain characters from my request?

I am doing a get request as follows using Restangular:


Restangular.all('details').get(detail_id).then(function(detailResult)
{ // etc...


However this works if detail_id is set to: 2019
but when detail_id is set to: 2019#1

Then Restangular (I assume) automatically strips off the #1 and I don't see this on the server side.

Am I making a mistake here or is this a limitation to Get requests with Restangular or alternatively is this working as intended and I have missed something?

Any help is appreciated,
Thanks

Answer

This is by design. The part after the # is called the fragment, and is it never sent to the server since it is supposed to be used on the client side only.

Per Wikipedia's article on the subject:

The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the web server

If # is actually part of the id, you'd need to url encode the parameter like this:

Restangular.all('details').get(encodeURIComponent(detail_id)).then(function(detailResult) { // etc...