0

I'm using Angular's Resource module to access a web API, but I'm having problems as the API uses URIs as the primary key.

Whenever I try to make a call to this API, passing in a URI as a string parameter, I'm getting 400 Bad Request errors. On closer inspection, Resource is escaping all the forward slashes in the URI but not the colon at the start. It's doing a GET on a URL that looks like this: http://myserver/api/objects/http:%2F%2Fexample.comk%2FmyURI%2F, which is of course invalid. I've also tried escaping the colon with a backslash, but that doesn't work either.

How can I make Resource escape my parameters properly? I've tried replacing the colon with %3A before making the call, but that results in the % being encoded again, returning 404 Not Found.

The service handling Resource looks like this:

angular.module('adminApp').factory('MyObject', myObject);

function myObject($resource) {
    return $resource('/api/objects/:uri');
};

and I'm calling it like this:

MyObject.get({ uri: myUri }, function(result) {
...
});
2
  • Did you try adding a backslash before the colon? Commented Jan 29, 2016 at 10:56
  • @Voreny Yes, which ends up looking like this: http://myserver/api/objects/http%5C:%2F%2Fexample.comk%2FmyURI%2F, so no better! Commented Jan 29, 2016 at 10:59

1 Answer 1

1

I've got around this issue by passing the URI as a query parameter instead of as part of the request URL. I did this by changing my resource service to this:

angular.module('adminApp').factory('MyObject', myObject);

function myObject($resource) {
    return $resource('/api/objects');
};

and leaving the calling code this the same. ngResource then creates a GET that looks like http://myserver/api/objects?uri=http:%2F%2Fexample.comk%2FmyURI%2F, which is fine.

Basically, if you're using unusual characters in your API parameters, put them in a query string rather than in the URL! :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.