<?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>DEV Community: ryan-cal</title>
    <description>The latest articles on DEV Community by ryan-cal (@ryancal).</description>
    <link>https://dev.to/ryancal</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%2F183159%2F073e40d8-5954-4176-8e8a-031a805764d8.png</url>
      <title>DEV Community: ryan-cal</title>
      <link>https://dev.to/ryancal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ryancal"/>
    <language>en</language>
    <item>
      <title>How to Use Axios to Make HTTP Requests – GET, POST and Parallel Requests</title>
      <dc:creator>ryan-cal</dc:creator>
      <pubDate>Fri, 21 Jun 2019 15:33:14 +0000</pubDate>
      <link>https://dev.to/ryancal/how-to-use-axios-to-make-http-requests-get-post-and-parallel-requests-3kn9</link>
      <guid>https://dev.to/ryancal/how-to-use-axios-to-make-http-requests-get-post-and-parallel-requests-3kn9</guid>
      <description>&lt;p&gt;Almost every web-based application that does anything substantial performs some kind of HTTP requests. In this article, we will be going over a very popular HTTP client for JavaScript called Axios. Axios is a very easy-to-use and convenient JavaScript library to perform HTTP requests in Node.js. Axios is actually a promise-based HTTP client library that works both in the browser and in a node.js environment. It basically provides a single API for dealing with XMLHttpRequests and node’s HTTP interface, making it very convenient to use. Below are some reason why you would want to use Axios over the traditional Fetch API&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  It supports older browsers seamlessly.&lt;/li&gt;
&lt;li&gt;  Has a way to set a response timeout and abort requests&lt;/li&gt;
&lt;li&gt;  It has built-in CSRF protection&lt;/li&gt;
&lt;li&gt;  Allows you to track upload progress&lt;/li&gt;
&lt;li&gt;  Performs automatic JSON data transformation&lt;/li&gt;
&lt;li&gt;  Works seamlessly in Node.js and Web Browsers&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Before we can start using Axios, we first need to install it. There are multiple ways of doing this: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using npm:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;axios
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;2. Using bower:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;bower &lt;span class="nb"&gt;install &lt;/span&gt;axios
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;3. Using Yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add axios
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;4. Including it in your page using unpkg.com:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="https://unpkg.com/axios/dist/axios.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;5. Manual download:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://github.com/mzabriskie/axios/tree/master/dist
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With the installation complete we are going to move on to perform requests. In this example below, we’ll be performing requests to the Calendarific API. We are using the Calendarific API. &lt;a href="https://calendarific.com/api-documentation"&gt;So get your API key here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://calendarific.com/"&gt;Calendarific&lt;/a&gt; provides a JSON API for fetching bank, public, local, national, holidays and observances information for over 200 countries for free. We will start with the most common HTTP methods GET and POST.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Performing a GET request&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;requestUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://calendarific.com/api/v2/holidays?country=US&amp;amp;amp;year=2018&amp;amp;amp;api&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s1"&gt;_key=hsy82&amp;amp;amp;type=national&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;requestUrl)
  .then(function(response){
    console.log(response.data); // ex.: { holidays: &lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="s1"&gt;}
    console.log(response.status); // ex.: 200
  });  

// Performing a POST request
axios.post(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;, { firstName: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;Marlon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;, lastName: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;Bernardes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; })
  .then(function(response){
    console.log(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;saved&lt;/span&gt; &lt;span class="nx"&gt;successfully&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;)
  });  
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Also, for convenience, you will generally use&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;axios.get()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;axios.post()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(like in jQuery you would use &lt;code&gt;$.get()&lt;/code&gt; and &lt;code&gt;$.post()&lt;/code&gt; instead of &lt;code&gt;$.ajax()&lt;/code&gt;) Axios offers methods for all the HTTP verbs, which are less popular but still used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;axios.delete()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;axios.put()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;axios.patch()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;axios.options()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and a method to get the HTTP headers of a request, discarding the body:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;axios.head()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Performing Multiple Parallel Requests simultaneously
&lt;/h2&gt;

&lt;p&gt;Another really powerful feature of Axios that cannot be understated is the ability to execute multiple requests in parallel, simply provide an array argument to &lt;code&gt;axios.all&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;When all requests are complete, you’ll receive an array containing the response objects in the same order they were sent. &lt;/p&gt;

&lt;p&gt;Alternatively, you can use axios.spread to spread the array into multiple arguments. Spread is preferred since dealing with array indexes could be misleading. &lt;/p&gt;

&lt;p&gt;In the example below will be using the &lt;code&gt;axios.all&lt;/code&gt; to fetch holidays for 4 countries simultaneously, UK, US, CA and Mexico and spreading the results in multiple variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Requests will be executed in parallel...&lt;/span&gt;
&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
   &lt;span class="c1"&gt;// Remember to replace the api\_key with a valid one.&lt;/span&gt;
    &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://calendarific.com/api/v2/holidays?country=US&amp;amp;year=2019&amp;amp;api&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s1"&gt;_key=hsy82&amp;amp;type=national&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
    &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hhttps://calendarific.com/api/v2/holidays?country=UK&amp;amp;year=2019&amp;amp;api&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s1"&gt;_key=hsy82&amp;amp;type=national&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hhttps://calendarific.com/api/v2/holidays?country=CA&amp;amp;year=2019&amp;amp;api&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s1"&gt;_key=hsy82&amp;amp;type=national&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hhttps://calendarific.com/api/v2/holidays?country=MX&amp;amp;year=2019&amp;amp;api&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s1"&gt;_key=hsy82&amp;amp;type=national&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;spread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;usHolidays&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ukHolidays&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;caHolidays&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mxHolidays&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//... but this callback will be executed only when all requests are complete.&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;United States Holidays in 2019&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;usHolidays&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UK in 2019&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ukHolidays&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Canada Holidays in 2019&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;caHolidays&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mexico Holidays in 2019&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mxHolidays&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, we will be going over how to send custom headers with Axios. To do this you need to supply an object containing the headers as the last argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;X-My-Custom-Header&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Header-Value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;axios.get('https://calendarific.com/api/v2/holidays', config);

//or
axios.post('/save', { firstName: 'Marlon' }, config);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://dotlayer.com/how-to-use-axios-to-make-http-requests-get-post-and-parallel-requests/"&gt;This article first appeared on Dotlayer.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>axios</category>
      <category>javascript</category>
      <category>calendarific</category>
    </item>
  </channel>
</rss>
