<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Lennart Johansson</title>
    <description>The latest articles on Forem by Lennart Johansson (@lennartjohansson).</description>
    <link>https://forem.com/lennartjohansson</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F333388%2F55762ed6-3bff-4d1c-93a6-0ce0d373165c.jpeg</url>
      <title>Forem: Lennart Johansson</title>
      <link>https://forem.com/lennartjohansson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lennartjohansson"/>
    <language>en</language>
    <item>
      <title>Calling a REST API with Angular 8</title>
      <dc:creator>Lennart Johansson</dc:creator>
      <pubDate>Tue, 11 Feb 2020 14:20:42 +0000</pubDate>
      <link>https://forem.com/lennartjohansson/calling-a-rest-api-with-angular-8-31ae</link>
      <guid>https://forem.com/lennartjohansson/calling-a-rest-api-with-angular-8-31ae</guid>
      <description>&lt;p&gt;The Angular team is releasing new versions of their framework at a high pace. New features are presented with every release and I'm currently waiting for version 9 where the Ivy renderer will be included in new projects by default. In the meantime I want to write my first post here on DEV covering one of the most basic but fundamental things in programming. Making a REST call.&lt;/p&gt;

&lt;p&gt;We will use a completely fresh Angular 8 install. You can either follow the instructions below or clone the &lt;a href="https://github.com/lennart-johansson/angular-8-rest-api-example"&gt;repository&lt;/a&gt; with the end result. &lt;/p&gt;

&lt;p&gt;First we install the Angular cli&lt;br&gt;
&lt;code&gt;sudo npm install -g @angular/cli&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then we make a new clean Angular app. You will be asked about routing and preferred styling. You can select no and css for the tutorial.&lt;br&gt;
&lt;code&gt;ng new angular-rest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng new&lt;/code&gt; creates a standard Angular app with all the default configurations. We should now be able to see the example page by serving the app. Hopefully it looks like the screenshot below.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd angular-rest&lt;/code&gt;&lt;br&gt;
&lt;code&gt;ng serve --open&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GXElfPb8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6tu5gbmdbpcrv1zh4szk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GXElfPb8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6tu5gbmdbpcrv1zh4szk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We now need an API to call. For this tutorial I will be using &lt;a href="https://mocki.io"&gt;Mocki&lt;/a&gt; to create a mock api. It's free and provides an easy way to quickly create a hosted mock api. Mocki gives you an url where there by default already is a Hello World response at the root. My url was&lt;br&gt;
&lt;code&gt;https://api.mocki.io/v1/ae70b3bc&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Remember your URL for upcomming steps.&lt;/p&gt;

&lt;p&gt;To make http calls with Angular we need to import the HttpClient in our app module &lt;code&gt;angular-rest\src\app\app.module.ts&lt;/code&gt;. After adding it the file should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X914ieFF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kooyfwe803bsftqdidwm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X914ieFF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kooyfwe803bsftqdidwm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can now inject the HttpClient in our future services. Services in Angular are used to call different parts of our REST API. We will create a service called HelloWorldService. Use the following angular cli command to create a service. The command will automatically create a services folder for us.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng g s services/hello-world&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The new service should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q0wwD3LJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dltp7v63ep6391dky1vk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q0wwD3LJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dltp7v63ep6391dky1vk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will now modify it a bit by including the HttpClient and also adding our mock url from &lt;a href="https://mocki.io"&gt;Mocki&lt;/a&gt;. It should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u2tv0u5B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/clblqsl6u43sxbvuappq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u2tv0u5B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/clblqsl6u43sxbvuappq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally we will modify our &lt;code&gt;angular-rest\src\app\app.component.ts&lt;/code&gt; to use our service. We will use the service to make a &lt;code&gt;getHelloWorld&lt;/code&gt;call and log it to the console. Lets implement the changes and see if it works!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YgpuEuSR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/puidpv5srjcgonvfhb19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YgpuEuSR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/puidpv5srjcgonvfhb19.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By checking the console in Chrome I can verify that we got a Hello World message! This was a super basic example of how to create a service in Angular 8 and use it to make a rest api call. If you want to read more about the HttpClient I recommend &lt;a href="https://angular.io/guide/http"&gt;Angulars documentation&lt;/a&gt;. Please feel free to leave a comment if you have any questions.&lt;/p&gt;

</description>
      <category>mock</category>
      <category>rest</category>
      <category>api</category>
      <category>angular</category>
    </item>
  </channel>
</rss>
