<?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: kristinenyaga</title>
    <description>The latest articles on DEV Community by kristinenyaga (@kristinenyaga).</description>
    <link>https://dev.to/kristinenyaga</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%2F920459%2Fd5449081-4251-480f-b577-05b8d9e45b9c.png</url>
      <title>DEV Community: kristinenyaga</title>
      <link>https://dev.to/kristinenyaga</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kristinenyaga"/>
    <language>en</language>
    <item>
      <title>Using Fetch To Make A Web Request</title>
      <dc:creator>kristinenyaga</dc:creator>
      <pubDate>Sun, 04 Sep 2022 21:12:26 +0000</pubDate>
      <link>https://dev.to/kristinenyaga/using-fetch-to-make-a-web-request-190f</link>
      <guid>https://dev.to/kristinenyaga/using-fetch-to-make-a-web-request-190f</guid>
      <description>&lt;h2&gt;
  
  
  What is Fetch?
&lt;/h2&gt;

&lt;p&gt;Fetch method provides an interface to access and manipulate resources across the web.&lt;/p&gt;

&lt;p&gt;A basic fetch is very simple since you only require the URL to the resource you want to get data from.&lt;br&gt;
Example&lt;br&gt;
&lt;code&gt;fetch("https://fakestoreapi.com/products")&lt;/code&gt;&lt;br&gt;
The fetch method returns a promise which brings us to the question,&lt;br&gt;
 &lt;strong&gt;What is a promise?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A promise is an assurance that the request is being processed and you will soon receive a response. After the fetch request is complete the promise is resolved with the response object.&lt;br&gt;
A JavaScript promise object can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Pending&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fulfilled&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rejected&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The promise object supports two states: State and Result.&lt;br&gt;
This can be demonstrated like so;&lt;br&gt;
Promise.state                        Promise.result &lt;br&gt;
"Pending"                            undefined&lt;br&gt;
"Fulfilled"                          a result value &lt;br&gt;
"Rejected"                           an error object&lt;/p&gt;
&lt;h2&gt;
  
  
  Reading the response
&lt;/h2&gt;

&lt;p&gt;After the fetch() is complete an object is given back as a response. The object can be read in a couple of ways:json,text.&lt;br&gt;
You chain the .then method to tell JavaScript that after completing the request convert the response object into a certain format.&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch("url")
.then(response =&amp;gt;response.json())
.then(data =&amp;gt; console.log(data))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Status code of the response
&lt;/h2&gt;

&lt;p&gt;The response object gives a status code of 200 and a status text of ok when a request is successful and 404 when an error was an error was encountered or if the requested resource doesn't exist.&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fetchText() {
    fetch("url")
    .then(response =&amp;gt; console.log(response.status) // 200
    if (response.status === 200) {
        .then(response =&amp;gt; response.json)
    }
}

fetchText();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are other types of status codes:&lt;br&gt;
500-occurs when requested url throws a server error.&lt;br&gt;
300-309-occurs when requested url is redirected to a new page with the response.&lt;/p&gt;

&lt;p&gt;The normal behaviour is GET we can override this by adding an object to the fetch object like so;&lt;br&gt;
&lt;code&gt;fetch("url",object)&lt;/code&gt;&lt;br&gt;
inside the object is where certain properties are defined&lt;/p&gt;
&lt;h2&gt;
  
  
  The properties
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Method&lt;/strong&gt;&lt;br&gt;
The request method eg GET,POST,PATCH,DELETE. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Headers&lt;/strong&gt;&lt;br&gt;
When you want to add headers to your request.&lt;br&gt;
It is contained within headers object.&lt;br&gt;
Example&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Content-Type and Accept-specifies content types of request body and output. Can be application/json for json files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Range-controls the pagination in list of API calls&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compression.used to compress the data in a response,it is done by setting the Accept-Encoding header&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Body&lt;/strong&gt;&lt;br&gt;
Here is where we pass whatever we want to send in an object format and then use the JSON.stringify method to convert the object to a string format so that the object can be sent across the web. When the content in the body arrives at the passed in url it is converted back to an object&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Catch&lt;/strong&gt;&lt;br&gt;
We add  a function called a catch(). This allows us to write code to "handle" the error when something goes wrong in a fetch() request.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const name = { username: 'example' };

//POST request with body equal on data in JSON format
fetch('https://example.com/profile', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(name),
})
.then((response) =&amp;gt; response.json())
//Then with the data from the response in JSON...
.then((data) =&amp;gt; {
  console.log('Success:', data);
})
//Then with the error genereted...
.catch((error) =&amp;gt; {
  console.error('Error:', error);
});

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

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
