angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

$resource strips port from url

Open thughes opened this issue 14 years ago • 36 comments

As discussed in https://groups.google.com/forum/?fromgroups#!topic/angular/18aO0bIlEm0%5B1-25%5D, if you include a port in the URL passed to $resource, it gets stripped since everything with a colon is substituted.

Escaping the colon works, but that makes it a pain to use the same URL with $http (which doesn't seem to understand the escaping).

In general you should be using relative URLs, but the use case for needing the full path is for cross origin requests.

thughes avatar Aug 08 '12 18:08 thughes

Is this a bug or... what? Seems redundant to #942

Or are you making a bug asking specifically what @JamesDunne proposed

ProLoser avatar Sep 05 '12 18:09 ProLoser

It's supposed to be a bug. It's the same as #942, but that issue was closed with "you need to use escaping". My point here was to illustrate that escaping isn't really a solution (every first time user of angular using a port number will hit it and $http doesn't understand the escaping so you can't use the same URL).

@JamesDunne's proposal seems fine, but I wasn't advocating any particular solution.

thughes avatar Sep 05 '12 18:09 thughes

I've just run into this problem too. Additionally, it doesn't seem to work escaping the colon with a backslash (using 1.0.2)

vitch avatar Sep 14 '12 15:09 vitch

The backslash escaping does not work for me; I'm using v1.0.2.

frapontillo avatar Sep 18 '12 07:09 frapontillo

I'm having the same problem: escaping : doesn't work and I'm using 1.0.2

Do you have found any solution?

Trying to access a url of this kind: http://subdomain.domain.com:9000/some/parameters

earlyriser avatar Oct 07 '12 03:10 earlyriser

A workaround would be to include a port parameter with a custom fixed value, such as http://subdomain.domain.com:port/some/parameters.

frapontillo avatar Oct 07 '12 08:10 frapontillo

Hi, that is really confusing problem. To fix that I was using {port} parameter in all resource instances as others do but, code gets really messy. So, I decided to have a single URL address in the configuration and came up with another solution, so try to use this:

$resource('http://localhost:XXXX\:XXXX/api/entities'); 

Test (in Coffeescript)

describe "Resource port replacment test", ->
  entity=null

  beforeEach module("YourApp")
  beforeEach inject(($resource, $httpBackend) ->
    @entity = $resource 'http://localhost:56789\:56789/api/entities'
    @httpBackend = $httpBackend
    @httpBackend.expectGET('http://localhost:56789/api/entities').
          respond(200, '1'))

  afterEach ->
    @httpBackend.verifyNoOutstandingExpectation()
    @httpBackend.verifyNoOutstandingRequest()

  it "checking the address", ->
    @entity.query {}
    @httpBackend.flush()

It's another trick, but works fine for me. Hope you guys can fix it in the next release.

Cheers

oilart avatar Oct 11 '12 11:10 oilart

There is a also a disparity between how $resource and $http handle this.

For example, if you use a common config variable with the double port (http://localhost:3000\:3000) as proposed above:

  • $http requests go to http://localhost:3000:3000
  • $resource requests go to http://localhost:3000

UPDATE: This was mentioned in the original ticket. Sorry for the repost, but it's a major pain for cross-domain and local development.

cmdrkeene avatar Oct 27 '12 19:10 cmdrkeene

Ran into this issue today, was able to solve with the following:

var escapedUrl = window.API_PREFIX.replace(/:([^\/])/, '\\:$1');

(Note: I'm sure that's not the best way to do it)

It seems that a : is a poor use for a string interpolation marker here since the chance of needing to specify hostname in the ajax call is high. Especially with the increasing popularity of projects completely decoupled from their associated API.

While I imagine regressions would make it infeasible to change at this point a much more acceptable token would be something like /api/{userId} which is pretty popular as well and unused in the URL. Colon was most likely chosen because it's a common paradigm in a lot of server-side URL dispatching and routing applications but the character is heavily reserved in URL schemes.

tlong-dev avatar Oct 30 '12 21:10 tlong-dev

You can escape port number using double \\, ex.: http://198.61.201.6\\:8000/api/v1-dev/trackers Here is the jsFiddle: http://jsfiddle.net/mkLzh/

Docs were updated in c398d7d3709333c4a228d955b5ee8cbcf4386468 thnx to @jsyrjala ! On-line docs should be updated soon as well.

pkozlowski-opensource avatar Dec 09 '12 16:12 pkozlowski-opensource

I think you can provide a better solution. The behaviour is different from $http. This is a workaround and very ugly. You should check "protocol://server:port". Are you really thinking that someone could use :port as variable? There are a lot of people asking for this problem. Don't fix it now, but keep it open please.

TLmaK0 avatar Jan 04 '13 10:01 TLmaK0

How about we disallow :identifiers that start with a digit? This is a pretty common restriction (see identifiers in JS itself, and most programming languages), so I don't think anyone will be caught by surprise. Real-world impact should also be minimal, I assume.

mernen avatar Jan 29 '13 04:01 mernen

:+1: On Jan 28, 2013 11:04 PM, "Daniel Luz" [email protected] wrote:

How about we disallow :identifiers that start with a digit? This is a pretty common restriction (see identifiers in JS itself, and most programming languages), so I don't think anyone will be caught by surprise. Real-world impact should also be minimal, I assume.

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1243#issuecomment-12819649.

cmdrkeene avatar Jan 29 '13 12:01 cmdrkeene

I'm going to :+1: this issue as this is still a pretty gnarly issue -- definitely bit a first time user like me. The reason I hit it is because I'm doing local development on a Chrome app, so it makes sense that my endpoint would be something like http://localhost:3000, no? Just seems bizarre that I should have to escape the port number. I like @mernen's solution.

nanodeath avatar Mar 17 '13 23:03 nanodeath

I'd like to :+1: @mernen's solution. This is a real pain in the ass and it requires some kludgey code to get $http and $resource using the same URL.

wildlyinaccurate avatar Apr 17 '13 18:04 wildlyinaccurate

Just another :+1: to say I've been hit by this moving between $resource and $http and wondering why things weren't working. Until I realised the port behaviour wasn't the same across them.

karlfreeman avatar May 01 '13 08:05 karlfreeman

Unfortunately the work around isn't feasible if you share URLs between $resource and $http.

We have a subdirectory for the production deployment, so the url needs to change to app.com/subdirectory. What we did to solve this is, instead of injecting a service to all factories that returns the full URL with the port for development, and the URL with the subdirectory for production, we are now just returning '/subdirectory' for production and an empty string for development, and the browser completes the URL path. It works great.

bbonamin avatar Jun 10 '13 15:06 bbonamin

As @bbonamin said, escaping the colon works for for $resource now. But it DOES NOT WORK for $http. So the issue is not fixed! Please reopen the issue and fix it in a way that enables us to use the same url for both. This is really a huge showstopper at dev time.

kdekooter avatar Jul 06 '13 06:07 kdekooter

I see that it might be a pain for people sharing the same URL across $http and $resource. @kdekooter I guess you will be motivated to send a pull request if this is a blocking point for you!

pkozlowski-opensource avatar Jul 06 '13 08:07 pkozlowski-opensource

@mernen has the best solution: let's change $resource so that patterns in URLs can't start with digit. I think that the right place to change code is somewhere around here https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js#L347

jsyrjala avatar Jul 06 '13 11:07 jsyrjala