<?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: Arisekola177</title>
    <description>The latest articles on DEV Community by Arisekola177 (@arisekola177).</description>
    <link>https://dev.to/arisekola177</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%2F636671%2F6cc8c8c8-e121-4e51-b4f3-f55670a54080.jpeg</url>
      <title>DEV Community: Arisekola177</title>
      <link>https://dev.to/arisekola177</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arisekola177"/>
    <language>en</language>
    <item>
      <title>MAKING API REQUEST WITH JAVASCRIPT</title>
      <dc:creator>Arisekola177</dc:creator>
      <pubDate>Fri, 24 Mar 2023 22:38:11 +0000</pubDate>
      <link>https://dev.to/arisekola177/making-api-request-with-javascript-37be</link>
      <guid>https://dev.to/arisekola177/making-api-request-with-javascript-37be</guid>
      <description>&lt;p&gt;Hello developers!!!!!&lt;/p&gt;

&lt;p&gt;This is the concluding part of my earlier post.&lt;/p&gt;

&lt;p&gt;In the earlier post, we discussed what APIs are, categories and also types of RESTful APIs.&lt;/p&gt;

&lt;p&gt;We will be discussing different methods of requesting APIs in this concluding part.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;XHR&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;XHR stands for XMLHttpRequest, which is a JavaScript API used to make asynchronous HTTP requests to a server.&lt;/p&gt;

&lt;p&gt;XHRs (XMLHttpRequests) are the oldest and have been used for many years to make HTTP requests from&lt;br&gt;
JavaScript. They are powerful and can be used for a wide range of purposes, but can be more difficult to use than newer APIs.&lt;/p&gt;

&lt;p&gt;XHRs allow web developers to update parts of a web page without requiring a full page reload. This is known as AJAX (Asynchronous JavaScript and XML) and enables more dynamic and responsive user interfaces.&lt;br&gt;
To make an XHR request, a developer creates a new XMLHttpRequest object, sets the request method (e.g. GET or POST), the URL of the resource to be accessed, and any additional parameters such as request headers or data to be sent. Then, the request is sent using the send) method of the XHR object.&lt;br&gt;
Once the server sends a response, the XHR object's “onload” method is called with the response data, which can then be processed and used to update the page.&lt;br&gt;
XHRs are often used in web applications to fetch data from a server without requiring a full page refresh, and can be used in conjunction with other web technologies such as JavaScript frameworks and APIs to create dynamic and responsive user interfaces.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to make API request with XHR.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;XMLHttpRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
          &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

     &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&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;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="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;ERROR&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="err"&gt;”&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="p"&gt;};&lt;/span&gt;

 &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;GET&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="s2"&gt;https://swapi.dev/api/people&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;The second method of making an API  call is with fetch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fetch is a modern JavaScript API used to make asynchronous HTTP requests to a server. Fetch is a newer alternative to the uolder XMLHttpRequest (XHR) API and is designed to be more flexible and easier to use.&lt;/p&gt;

&lt;p&gt;Fetch is a newer API that is designed to be more flexible and easier to use than XHRs.&lt;br&gt;
Fetch provides a simple and consistent&lt;br&gt;
API for making HTTP requests, and includes modern features such as support for Promises and streaming responses.&lt;/p&gt;

&lt;p&gt;To make a fetch request, a developer creates a new 'Request object with the URL of the resource to be accessed and any additional parameters such as request headers or data to be sent. Then, the fetch() method is called with the 'Request object as an argument.&lt;br&gt;
Once the server sends a response, the&lt;br&gt;
'fetch() method returns a Promise object representing the response. This Promise object can then be used to access the response data and any relevant metadata such as the response headers.&lt;br&gt;
One of the key benefits of fetch is that it supports modern features such as CORS&lt;br&gt;
(Cross-Origin Resource Sharing) out of the box, making it easier to access resources on different domains.&lt;br&gt;
Fetch also supports streaming responses and the ability to abort requests, and can be used in conjunction with other web technologies such as JavaScript frameworks and APls to create dynamic and responsive user interfaces.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to make an API request with fetch.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;starWarsPeople&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="na"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//swapi.dev/api/people’);&lt;/span&gt;

 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&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="nx"&gt;res&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;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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="err"&gt;‘&lt;/span&gt;&lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;!!!&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&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;starWarsPeople&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;The last method we will be discussing is axios.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Axios is a popular JavaScript library used to make HTTP requests from a web browser or&lt;br&gt;
Node.js server. Axios provides a simple and powerful API for making HTTP requests, and is designed to be easy to use and highly configurable.&lt;/p&gt;

&lt;p&gt;Axios supports a wide range of HTTP request methods such as GET, POST, PUT, DELETE, and PATCH, and also provides a range of options for customizing request headers and handling responses.&lt;br&gt;
To make a request with Axios, a developer creates a new instance of the Axios library and then calls one of its methods (such as&lt;br&gt;
"get ()", post ()", etc.) with the URL of the resource to be accessed and any additional parameters such as request headers or data to be sent. Once the server sends a response, Axios returns a 'Promise" object representing the response, which can then be used to access the response data and any relevant metadata such as the response headers.&lt;br&gt;
Axios also includes features such as automatic transformation of request and response data, support for request and response interceptors, and support for canceling requests. These features make it a powerful and flexible library for making HTTP requests in modern web applications.&lt;br&gt;
Overall, Axios is a widely used and highly regarded library for making HTTP requests in JavaScript-based applications, due to its simplicity, flexibility, and ease of use.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Making request with axios.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The below cdn should be included in the HTML file in the script tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//cdn.jsdelivr.net/npm/axios/dist/axios.min.js”&amp;gt;&amp;lt;script&amp;gt;.&lt;/span&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getStarWarsPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&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="err"&gt;‘&lt;/span&gt;&lt;span class="na"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//swapi.dev/api/people’);&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="nx"&gt;res&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;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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="err"&gt;‘&lt;/span&gt;&lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;!!!&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&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;getStarWarsPerson&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;-WRAP UP!!!!&lt;/p&gt;

&lt;p&gt;Thank you for your time.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>api</category>
      <category>node</category>
    </item>
    <item>
      <title>MAKING API REQUEST WITH JAVASCRIPT.</title>
      <dc:creator>Arisekola177</dc:creator>
      <pubDate>Fri, 24 Mar 2023 09:50:36 +0000</pubDate>
      <link>https://dev.to/arisekola177/making-api-request-with-javascript-3ja6</link>
      <guid>https://dev.to/arisekola177/making-api-request-with-javascript-3ja6</guid>
      <description>&lt;p&gt;Hello developers! In this post, we’ll discuss various methods to request an API for your next project.&lt;/p&gt;

&lt;p&gt;In the chapter 1 of this post, we’ll discuss what APIs means, it’s categories and types.&lt;/p&gt;

&lt;p&gt;Then in the chapter 2, we’ll discuss various methods in making API calls with JavaScript.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;WHAT ARE APIs???&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;API stands for Application Programming Interface. An API is a set of protocols, routines and tools for building software application. In essence, an API specifies how software components should interact with each other.&lt;/p&gt;

&lt;p&gt;They are commonly used to access web-based services and databases.&lt;/p&gt;

&lt;p&gt;APIs can be public or private. Public APIs are available to developers and users outside of the organization that developed the API, while private APIs are designed for internal use only.&lt;/p&gt;

&lt;p&gt;APIs are often categorized by the way they are accessed such as RESTful APIs, which are accessed using HTTP requests, or SOAP APIs, which use XML to communicate.&lt;/p&gt;

&lt;p&gt;We will only discuss RESTful APIs in this post due to modern usage.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;WHAT ARE RESTful APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A RESTful API is a type of API that uses HTTP request to perform create, read, update and delete(CRUD) operations on resources.&lt;/p&gt;

&lt;p&gt;RESTful API is an interface that two computer systems use to exchange information securely over the internet. Most business applications have to communicate with other internal and third-party applications to perform various tasks.&lt;/p&gt;

&lt;p&gt;REST stands for Representational state transfer, which is a set of architectural principles to building web services.&lt;/p&gt;

&lt;p&gt;In a RESTful API each resources is identified by a unique URL and HTTP methods such as GET, POST, PUT and DELETE are used to interact with the resources. For example, a GET request to a URL that represents a customer resource might return information about that customer, while a POST request to the same URL might create a new customer.&lt;/p&gt;

&lt;p&gt;In addition, RESTful APIs of teen use standard HTTP status codes to indicate the success of failure of a request. For example, a 200 status code indicate a successful request, while a 404 status code indicates that the requested resource was not found.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TYPES&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are several types of RESTful APIs, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Hypermedia APIs which uses APIs such as links and other navigational elements to allow clients to discover and navigate the APIs resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Search-bases APIs which allow clients to search for resources based on various criteria. They typically provide a search endpoint that accept query parameters and returns a set of results based on the search criteria. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Others are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Paginated APIs&lt;/li&gt;
&lt;li&gt;Streaming APIs&lt;/li&gt;
&lt;li&gt;File-based APIs&lt;/li&gt;
&lt;li&gt;CRUD-based APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks so much for your time. The chapter 2 will be posted as soon as possible.&lt;/p&gt;

</description>
      <category>api</category>
      <category>javascript</category>
      <category>node</category>
      <category>html</category>
    </item>
  </channel>
</rss>
