0

I have inherited an AngularJS project, to integrate it with the RESTful server I have been working on. The client side code hits the relative urls($http.get('/relative/url')) of the REST API, and need to configure it hit the proper base url.

Judging from the docs, https://docs.angularjs.org/api/ng/service/$http, specifying the relative url like that is completely expected, however I can't seem to gather from the docs, or anywhere else, how one goes about specifying the base endpoint to use.

1 Answer 1

1

There is no way to natively do this within $http. You would have to manually specify the full URL.

One solution is to create a base service that is implemented in other services so that your full-path base url is maintained in a single location, rather than spreading it out across multiple services.

Once you have a full-path url, simply use it like any relative-path url:

$http.get('http://yourdomain.com/restapi/url')
Sign up to request clarification or add additional context in comments.

7 Comments

In addition to what @David L has mentioned, you could create a constant baseUrl and add it to the method. Depending on the production or dev envinronment you could change your baseUrl
@Bazinga777 Correct and (hopefully) manage this configuration constant via your automated continuous integration release process.
@DavidL I am aware that I can specify the absolute url, by reading from a config somewhere and then concatenating that base url elsewhere. How is the relative url used then?
@BrentHronik You have two options. Concatenate the base and relative urls to form a full absolute url or simply use the relative url.
@BrentHronik it is relative to the root of your website, which, depending on how you are hosting, is typically your domain name. So, for instance, if your site is www.awesomeness.com, your relative path is '/api/customers', which, when fully resolved, would be www.awesomeness.com/api/customers.
|

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.