SlideShare a Scribd company logo
Axios
• Axios is a promise-based HTTP Client for node.js and the browser.
On the server-side it uses the native node.js http module, while on
the client (browser) it uses XMLHttpRequests.
• Axios is a Javascript library that allows you to connect with the
backend API and manage requests made via the HTTP protocol.
• It also provides a more flexible and powerful feature set.
• It is similar to the native JavaScript Fetch API Method.
Prerequisites
Let's consider some prerequisites before proceeding.
• Familiar with yarn or node npm package module.
• Create react app tool pre-installed.
• Basic knowledge of HTML/CSS and Javascript(ES6).
• Basic understanding of command-line terminal.
Installing Axios
• In order to use Axios with React, we need to install Axios. It does not
come as a native JavaScript API, so that's why we have to manually
import into our project.
• There are many ways to install Axios. You can pick any of them based
on your system environment.
• Open up a new terminal window, move to your project’s root
directory, and run any of the following commands to add Axios to
your project.
• Using npm: $ npm install axios
• Using bower package manager: $ bower install axios
• Using yarn: $ yarn add axios
• Using unpkg CDN: <script
src="https://unpkg.com/axios/dist/axios.min.js"></script>
• Using jsDelivr CDN: <script
src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
Features
• Make XMLHttpRequests from the browser
• Make http requests from node.js
• Supports the Promise API
• Intercept request and response
• Transform request and response data
• Cancel requests
• Automatic transforms for JSON data
• Client side support for protecting against XSRF
Looking at the response object
When we send a request to server and it is successful, our then() callback will
receive a response object which can have the following properties:
• data: It is the payload returned from the server. By default, Axios expects JSON
and will parse this back into a JavaScript object for you.
• status: It is basically the HTTP code returned from the server.
• statusText: it refers to the HTTP status message returned by the server.
• headers: It contains all the headers sent back by the server.
• config: It has the original request configuration.
• request: It is the actual XMLHttpRequest object (when it is running in a browser)
Looking at the error object
And if there’s a problem with the request, the promise will be
rejected with an error object containing at least one or few of the
following properties:
• message: the error message text.
• response: the response object (if received) as described in the
previous section.
• request: the actual XMLHttpRequest object (when running in a
browser).
• config: the original request configuration.
Axios status code
HTTP response status codes indicate whether a specific HTTP request has
been successfully completed. Responses are grouped in five classes:
• Informational responses (100–199)
• Successful responses (200–299)
• Redirects (300–399)
• Client errors (400–499)
• Server errors (500–599)
Shorthand Methods in Axios
You have already seen that Axios provides an easy-to-use API contract model in a compact package for most of
our HTTP communication needs.
Do you know that Axios has some shorthand methods for performing different HTTP requests?
Check them below:
• axios.request(config)
• axios.get(url[, config])
• axios.delete(url[, config])
• axios.head(url[, config])
• axios.options(url[, config])
• axios.post(url[, data[, config]])
• axios.put(url[, data[, config]])
• axios.patch(url[, data[, config]])
Axios :-
PROS:
• Promise based HTTP client for the browser and server (NodeJS)
• Compatible with some older browsers (ie. IE11)
• Provides Cancelling Request ⇒ that can also be done without Axios using AbortController)
• Very popular third-party library actively supported
• Wrapper / decorator pattern which offers nice and convenient interface
CONS:
• Must be installed and imported (not native in JavaScript)
• Not the standard so it is important to manage the conflict possibilities
• Third-party libraries adds weight/load on the application or website (to be considered)
Diffrence B/w Axios and Fetch
• Axios • Fetch
• Axios has url in request object. • Fetch has no url in request object.
• Axios is a stand-alone third party package that can be easily
installed.
• Fetch is built into most modern browsers; no installation is
required as such.
• Axios enjoys built-in XSRF protection. • Fetch does not.
• Axios uses the data property. • Fetch uses the body property.
• Axios’ data contains the object. • Fetch’s body has to be stringified.
• Axios request is ok when status is 200 and statusText is ‘OK’.
• Fetch request is ok when response object contains the ok
property.
• Axios performs automatic transforms of JSON data.
• Fetch is a two-step process when handling JSON data- first, to
make the actual request; second, to call the .json() method on
the response.
• Axios allows cancelling request and request timeout. • Fetch does not.
• Axios has the ability to intercept HTTP requests. • Fetch, by default, doesn’t provide a way to intercept requests.
• Axios has built-in support for download progress. • Fetch does not support upload progress.
• Axios has wide browser support.
• Fetch only supports Chrome 42+, Firefox 39+, Edge 14+, and
Safari 10.1+ (This is known as Backward Compatibility).
Simple GET request - Axios vs Fetch
Axios GET request:
1. // Simple GET request using axios
2. Const element = document.querySelector('#get-request .axios.total’);
3. axios.get('https://reqres.in/api/users’)
4. .then(response => element.innerHTML = response.data.total);
Fetch GET request
1. // Simple GET request using fetch
2. const element = document.querySelector('#get-request .fetch .total’);
3. fetch('https://reqres.in/api/users’)
4. .then(response => response.json())
5. .then(data => element.innerHTML = data.total );

More Related Content

What's hot

React workshop
React workshopReact workshop
React workshop
Imran Sayed
 
Express js
Express jsExpress js
Express js
Manav Prasad
 
Solid NodeJS with TypeScript, Jest & NestJS
Solid NodeJS with TypeScript, Jest & NestJSSolid NodeJS with TypeScript, Jest & NestJS
Solid NodeJS with TypeScript, Jest & NestJS
Rafael Casuso Romate
 
React Context API
React Context APIReact Context API
React Context API
NodeXperts
 
React for Beginners
React for BeginnersReact for Beginners
React for Beginners
Derek Willian Stavis
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
Varun Raj
 
Learn react-js
Learn react-jsLearn react-js
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
Hùng Nguyễn Huy
 
Intro to React
Intro to ReactIntro to React
Intro to React
Justin Reock
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
Visual Engineering
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
ivpol
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
Emanuele DelBono
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
Sergey Romaneko
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
07.pallav
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
ANKUSH CHAVAN
 
React state
React  stateReact  state
React state
Ducat
 
React js
React jsReact js
Spring mvc
Spring mvcSpring mvc
Spring mvc
Pravin Pundge
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha Gadkari
Surekha Gadkari
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
DivyanshGupta922023
 

What's hot (20)

React workshop
React workshopReact workshop
React workshop
 
Express js
Express jsExpress js
Express js
 
Solid NodeJS with TypeScript, Jest & NestJS
Solid NodeJS with TypeScript, Jest & NestJSSolid NodeJS with TypeScript, Jest & NestJS
Solid NodeJS with TypeScript, Jest & NestJS
 
React Context API
React Context APIReact Context API
React Context API
 
React for Beginners
React for BeginnersReact for Beginners
React for Beginners
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
Learn react-js
Learn react-jsLearn react-js
Learn react-js
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
 
React state
React  stateReact  state
React state
 
React js
React jsReact js
React js
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha Gadkari
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 

Similar to Presentation1.pptx

Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
Nir Elbaz
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
ERRest and Dojo
ERRest and DojoERRest and Dojo
ERRest and Dojo
WO Community
 
Ajax
AjaxAjax
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
Ganesh Chavan
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP Tutorial
Lorna Mitchell
 
JavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User ExperienceJavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User Experience
reeder29
 
Web Technologies in Java EE 7
Web Technologies in Java EE 7Web Technologies in Java EE 7
Web Technologies in Java EE 7
Lukáš Fryč
 
Top 10 HTML5 Features for Oracle Cloud Developers
Top 10 HTML5 Features for Oracle Cloud DevelopersTop 10 HTML5 Features for Oracle Cloud Developers
Top 10 HTML5 Features for Oracle Cloud Developers
Brian Huff
 
Building Client-Side Attacks with HTML5 Features
Building Client-Side Attacks with HTML5 FeaturesBuilding Client-Side Attacks with HTML5 Features
Building Client-Side Attacks with HTML5 Features
Conviso Application Security
 
Windows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside worldWindows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside world
Prabhakaran Soundarapandian
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
Tiago Knoch
 
Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...
Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...
Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...
Lviv Startup Club
 
Android volley
Android volleyAndroid volley
Android volley
Programming Talents
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
Web-Socket
Web-SocketWeb-Socket
Websocket
WebsocketWebsocket
Websocket
艾鍗科技
 
Resting on your laurels will get you powned
Resting on your laurels will get you pownedResting on your laurels will get you powned
Resting on your laurels will get you powned
Dinis Cruz
 
Coding 100-session-slides
Coding 100-session-slidesCoding 100-session-slides
Coding 100-session-slides
Cisco DevNet
 
WEB TECHNOLOGY Unit-5.pptx
WEB TECHNOLOGY Unit-5.pptxWEB TECHNOLOGY Unit-5.pptx
WEB TECHNOLOGY Unit-5.pptx
karthiksmart21
 

Similar to Presentation1.pptx (20)

Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
ERRest and Dojo
ERRest and DojoERRest and Dojo
ERRest and Dojo
 
Ajax
AjaxAjax
Ajax
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP Tutorial
 
JavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User ExperienceJavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User Experience
 
Web Technologies in Java EE 7
Web Technologies in Java EE 7Web Technologies in Java EE 7
Web Technologies in Java EE 7
 
Top 10 HTML5 Features for Oracle Cloud Developers
Top 10 HTML5 Features for Oracle Cloud DevelopersTop 10 HTML5 Features for Oracle Cloud Developers
Top 10 HTML5 Features for Oracle Cloud Developers
 
Building Client-Side Attacks with HTML5 Features
Building Client-Side Attacks with HTML5 FeaturesBuilding Client-Side Attacks with HTML5 Features
Building Client-Side Attacks with HTML5 Features
 
Windows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside worldWindows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside world
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 
Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...
Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...
Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...
 
Android volley
Android volleyAndroid volley
Android volley
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
 
Web-Socket
Web-SocketWeb-Socket
Web-Socket
 
Websocket
WebsocketWebsocket
Websocket
 
Resting on your laurels will get you powned
Resting on your laurels will get you pownedResting on your laurels will get you powned
Resting on your laurels will get you powned
 
Coding 100-session-slides
Coding 100-session-slidesCoding 100-session-slides
Coding 100-session-slides
 
WEB TECHNOLOGY Unit-5.pptx
WEB TECHNOLOGY Unit-5.pptxWEB TECHNOLOGY Unit-5.pptx
WEB TECHNOLOGY Unit-5.pptx
 

Recently uploaded

Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 

Recently uploaded (20)

Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 

Presentation1.pptx

  • 1. Axios • Axios is a promise-based HTTP Client for node.js and the browser. On the server-side it uses the native node.js http module, while on the client (browser) it uses XMLHttpRequests. • Axios is a Javascript library that allows you to connect with the backend API and manage requests made via the HTTP protocol. • It also provides a more flexible and powerful feature set. • It is similar to the native JavaScript Fetch API Method.
  • 2. Prerequisites Let's consider some prerequisites before proceeding. • Familiar with yarn or node npm package module. • Create react app tool pre-installed. • Basic knowledge of HTML/CSS and Javascript(ES6). • Basic understanding of command-line terminal.
  • 3. Installing Axios • In order to use Axios with React, we need to install Axios. It does not come as a native JavaScript API, so that's why we have to manually import into our project. • There are many ways to install Axios. You can pick any of them based on your system environment. • Open up a new terminal window, move to your project’s root directory, and run any of the following commands to add Axios to your project.
  • 4. • Using npm: $ npm install axios • Using bower package manager: $ bower install axios • Using yarn: $ yarn add axios • Using unpkg CDN: <script src="https://unpkg.com/axios/dist/axios.min.js"></script> • Using jsDelivr CDN: <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  • 5. Features • Make XMLHttpRequests from the browser • Make http requests from node.js • Supports the Promise API • Intercept request and response • Transform request and response data • Cancel requests • Automatic transforms for JSON data • Client side support for protecting against XSRF
  • 6. Looking at the response object When we send a request to server and it is successful, our then() callback will receive a response object which can have the following properties: • data: It is the payload returned from the server. By default, Axios expects JSON and will parse this back into a JavaScript object for you. • status: It is basically the HTTP code returned from the server. • statusText: it refers to the HTTP status message returned by the server. • headers: It contains all the headers sent back by the server. • config: It has the original request configuration. • request: It is the actual XMLHttpRequest object (when it is running in a browser)
  • 7. Looking at the error object And if there’s a problem with the request, the promise will be rejected with an error object containing at least one or few of the following properties: • message: the error message text. • response: the response object (if received) as described in the previous section. • request: the actual XMLHttpRequest object (when running in a browser). • config: the original request configuration.
  • 8. Axios status code HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes: • Informational responses (100–199) • Successful responses (200–299) • Redirects (300–399) • Client errors (400–499) • Server errors (500–599)
  • 9. Shorthand Methods in Axios You have already seen that Axios provides an easy-to-use API contract model in a compact package for most of our HTTP communication needs. Do you know that Axios has some shorthand methods for performing different HTTP requests? Check them below: • axios.request(config) • axios.get(url[, config]) • axios.delete(url[, config]) • axios.head(url[, config]) • axios.options(url[, config]) • axios.post(url[, data[, config]]) • axios.put(url[, data[, config]]) • axios.patch(url[, data[, config]])
  • 10. Axios :- PROS: • Promise based HTTP client for the browser and server (NodeJS) • Compatible with some older browsers (ie. IE11) • Provides Cancelling Request ⇒ that can also be done without Axios using AbortController) • Very popular third-party library actively supported • Wrapper / decorator pattern which offers nice and convenient interface CONS: • Must be installed and imported (not native in JavaScript) • Not the standard so it is important to manage the conflict possibilities • Third-party libraries adds weight/load on the application or website (to be considered)
  • 11. Diffrence B/w Axios and Fetch • Axios • Fetch • Axios has url in request object. • Fetch has no url in request object. • Axios is a stand-alone third party package that can be easily installed. • Fetch is built into most modern browsers; no installation is required as such. • Axios enjoys built-in XSRF protection. • Fetch does not. • Axios uses the data property. • Fetch uses the body property. • Axios’ data contains the object. • Fetch’s body has to be stringified. • Axios request is ok when status is 200 and statusText is ‘OK’. • Fetch request is ok when response object contains the ok property. • Axios performs automatic transforms of JSON data. • Fetch is a two-step process when handling JSON data- first, to make the actual request; second, to call the .json() method on the response. • Axios allows cancelling request and request timeout. • Fetch does not. • Axios has the ability to intercept HTTP requests. • Fetch, by default, doesn’t provide a way to intercept requests. • Axios has built-in support for download progress. • Fetch does not support upload progress. • Axios has wide browser support. • Fetch only supports Chrome 42+, Firefox 39+, Edge 14+, and Safari 10.1+ (This is known as Backward Compatibility).
  • 12. Simple GET request - Axios vs Fetch Axios GET request: 1. // Simple GET request using axios 2. Const element = document.querySelector('#get-request .axios.total’); 3. axios.get('https://reqres.in/api/users’) 4. .then(response => element.innerHTML = response.data.total); Fetch GET request 1. // Simple GET request using fetch 2. const element = document.querySelector('#get-request .fetch .total’); 3. fetch('https://reqres.in/api/users’) 4. .then(response => response.json()) 5. .then(data => element.innerHTML = data.total );