{"id":148,"date":"2026-05-06T08:52:14","date_gmt":"2026-05-06T08:52:14","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=148"},"modified":"2026-05-06T08:52:15","modified_gmt":"2026-05-06T08:52:15","slug":"fetch-api","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=fetch-api","title":{"rendered":"Fetch API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Fetch API is a modern JavaScript feature used to make network requests to servers. It allows developers to retrieve and send data asynchronously without reloading the web page. Fetch is widely used for working with APIs and handling dynamic data in web applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Core Purpose of Fetch API<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Fetch API helps developers communicate with servers by sending requests and receiving responses. It is mainly used to load data from external sources such as databases or third party APIs and display it on a website in real time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How Fetch API Works<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Fetch uses promises to handle asynchronous operations. When a request is sent, it returns a promise that resolves once the response is received. This makes it easier to manage data and avoid blocking the user interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Basic Syntax Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fetch(\"https:\/\/api.example.com\/data\")<br>  .then(response =&gt; response.json())<br>  .then(data =&gt; {<br>    console.log(data);<br>  })<br>  .catch(error =&gt; {<br>    console.log(\"Error:\", error);<br>  });<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Making Different Types of Requests<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">GET Request<br>Used to retrieve data from a server<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fetch(\"https:\/\/api.example.com\/users\")<br>  .then(res =&gt; res.json())<br>  .then(data =&gt; console.log(data));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">POST Request<br>Used to send data to a server<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fetch(\"https:\/\/api.example.com\/users\", {<br>  method: \"POST\",<br>  headers: {<br>    \"Content-Type\": \"application\/json\"<br>  },<br>  body: JSON.stringify({<br>    name: \"Ali\",<br>    age: 22<br>  })<br>})<br>.then(res =&gt; res.json())<br>.then(data =&gt; console.log(data));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">PUT Request<br>Used to update existing data<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fetch(\"https:\/\/api.example.com\/users\/1\", {<br>  method: \"PUT\",<br>  body: JSON.stringify({ name: \"Ahmed\" }),<br>  headers: {<br>    \"Content-Type\": \"application\/json\"<br>  }<br>});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">DELETE Request<br>Used to remove data<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fetch(\"https:\/\/api.example.com\/users\/1\", {<br>  method: \"DELETE\"<br>});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Handling Errors<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Error handling is important when working with APIs. Fetch only rejects a promise on network failure, so developers must manually check response status.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fetch(\"https:\/\/api.example.com\/data\")<br>  .then(response =&gt; {<br>    if (!response.ok) {<br>      throw new Error(\"Request failed\");<br>    }<br>    return response.json();<br>  })<br>  .then(data =&gt; console.log(data))<br>  .catch(error =&gt; console.log(error));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Advantages of Fetch API<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Simple and clean syntax<br>Built into modern browsers<br>Uses promises for better readability<br>Supports asynchronous programming<br>Flexible for different request types<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Fetch API is an essential tool in modern web development. It allows developers to interact with servers efficiently and build dynamic applications that update data in real time. Learning Fetch API improves your ability to work with APIs and create responsive web experiences.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced JavaScript > Asynchronous JavaScript > Fetch API<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1778057513781\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1778057513558\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-148","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Fetch API - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn Fetch API in JavaScript to send and receive data from servers using GET POST PUT DELETE methods easily and efficiently\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/javascript\/?lesson=fetch-api\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Fetch API - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn Fetch API in JavaScript to send and receive data from servers using GET POST PUT DELETE methods easily and efficiently\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=fetch-api\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-06T08:52:15+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=fetch-api\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=fetch-api\",\"name\":\"Fetch API - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-06T08:52:14+00:00\",\"dateModified\":\"2026-05-06T08:52:15+00:00\",\"description\":\"Learn Fetch API in JavaScript to send and receive data from servers using GET POST PUT DELETE methods easily and efficiently\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=fetch-api#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=fetch-api\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=fetch-api#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced JavaScript > Asynchronous JavaScript > Fetch API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Fetch API - Learn JavaScript with GIGZ.PK","description":"Learn Fetch API in JavaScript to send and receive data from servers using GET POST PUT DELETE methods easily and efficiently","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/javascript\/?lesson=fetch-api","og_locale":"en_US","og_type":"article","og_title":"Fetch API - Learn JavaScript with GIGZ.PK","og_description":"Learn Fetch API in JavaScript to send and receive data from servers using GET POST PUT DELETE methods easily and efficiently","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=fetch-api","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-06T08:52:15+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javascript\/?lesson=fetch-api","url":"https:\/\/gigz.pk\/javascript\/?lesson=fetch-api","name":"Fetch API - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-06T08:52:14+00:00","dateModified":"2026-05-06T08:52:15+00:00","description":"Learn Fetch API in JavaScript to send and receive data from servers using GET POST PUT DELETE methods easily and efficiently","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=fetch-api#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=fetch-api"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=fetch-api#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Advanced JavaScript > Asynchronous JavaScript > Fetch API"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}