Angular http (minimal example)

Example

When interacting with server you have nowdays two options that are battle tested. Websockets and http requests. This example is focused on angular http requests. We will be doing http request to server that will return “Hello World” for us as is in this plunker:

How Does It Work

So first of all we will start with our api. I’m quite lazy so I will use apiary.io to return {“data”: “Hello World”}. Now when we have api we will use angular Http module in our component. But you should know that this logic belongs to the service, handling http requests from component is bad practice and I do so for sake of simplicity. In requestGreeting() method we sent GET request to the server and we subscribe for the response. The .subscribe() method should be called on the end of the chain. If you want to do some aggregation on response you can use .map() and again this should be done in service.

Side Notes

OK, few side notes don’t mix http and https content you may end up with Mixed Content Error. And again you should always handle http requests from service.

You can now go to the Angular Docs for HttpClient for a more in depth overview of Http.

Buy me a coffeeBuy me a coffee