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.

Related Post

Right Click to Create New File on Mac OS X TL;DR; Windows has a lot of cons but it has also some pros. One of them is a smooth way to create empty file on right-click. Mac OS doesn't come with...
Free Better Battery Stats and Time Remaining for M... Better Battery Stats and Time Remaining for Mac OS TL;DR; Do you miss the time remaining indicator on your MacBook, MacBook Air, or MacBook Pro? He...
Convert HEIC to PNG on Right Click for Free Convert HEIC to PNG on Right-Click In iOS 11 Apple is replacing the PNG image format with the new HEIC alternative. But what to do when device or pr...
Right-Click to Translate (Look Up) with Google Tra... If you are not native english speaker, you may be frustrated with default "look up" options you get after selecting a text and right clicking. There a...
PPTP VPN on MAC OS X Sierra, High Sierra and Mojav... PPTP VPN for Mac OS X Introduction PPTP VPN is no longer supported on macOS Sierra and High Sierra and that is just fine, because there are some se...
FAQ: Mac OS X Services HEIC to JPG on Right-Click Service Q: Where’d all my services go? I don’t see anything in the Services menu. A: If you don’t select anything before...
Buy me a coffeeOut of coffee 😱, please help!