Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular trailing slash for resource

Tags:

angularjs

My api requires a trailing slash for api calls. I was wondering how to pull that off with angular.

So i need to be able to access /tasks/ or a /tasks/xxxx/. I attempted to do it via:

angular.module('taskServices', ['ngResource']).         factory('Tasks', function($resource){             return $resource('/tasks/:task_id/', {}, {                  query: {method:'GET',                           params:{},                          isArray:true}             });  }); 

and a

$scope.tasks = Tasks.query();  

But it results in a /tasks or a tasks/xxx query.

How can i force it to always be /tasks/ and /tasks/xxx/

like image 769
Nix Avatar asked Jan 26 '13 02:01

Nix


People also ask

Should API have trailing slash?

Always use trailing slashes for any resource that may contain children. Just consider "GET" on a public_html directory with files.

How do you fix trailing slash issues?

A 301 redirect is the best way to resolve duplicate content issues caused by trailing slashes. If you're just fixing one page, you'd redirect the duplicate copy to the version that matches your chosen URL structure. Most trailing slash issues however, affect many pages across a website.

What is a trailing backslash?

Historically, it's common for URLs with a trailing slash to indicate a directory, and those without a trailing slash to denote a file: http://example.com/foo/ (with trailing slash, conventionally a directory) http://example.com/foo (without trailing slash, conventionally a file)

How do I get rid of trailing slash?

Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.


2 Answers

This seems to have been fixed: https://github.com/angular/angular.js/pull/5560

You can now configure it this way:

app.config(function($resourceProvider) {   $resourceProvider.defaults.stripTrailingSlashes = false; }); 
like image 173
Marco Fucci Avatar answered Sep 20 '22 21:09

Marco Fucci


The trailing slash is explicitly removed in this line of AngularJS source code. I'm not fully sure what was the rationale behind this code but there is already an issue opened for this: https://github.com/angular/angular.js/issues/992

As noted in the mentioned issue the $resource factory is great for RESTful endpoints that conform to a certain specification. While $resource will do do great job talking to back-ends conforming to a this specification, it has limitations that might rule it out for back-ends that don't obey a contract expected by the $resource. In such a case the best way forward is to switch to using the lower-level $http service as noted in this question: Recommended way of getting data from the server

$http is a very powerful and flexible service and allows full control over URLs, parameters sent etc.

like image 27
pkozlowski.opensource Avatar answered Sep 20 '22 21:09

pkozlowski.opensource