SlideShare a Scribd company logo
1 of 12
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

ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentationThanh Tuong
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JSArno Lordkronos
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJSHoang Long
 
What is component in reactjs
What is component in reactjsWhat is component in reactjs
What is component in reactjsmanojbkalla
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React NativeSambhu Lakshmanan
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The BasicsJeff Fox
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react formYao Nien Chung
 
Best Practice-React
Best Practice-ReactBest Practice-React
Best Practice-ReactYang Yang
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
Meetup angular http client
Meetup angular http clientMeetup angular http client
Meetup angular http clientGaurav Madaan
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation洪 鹏发
 

What's hot (20)

ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
What is component in reactjs
What is component in reactjsWhat is component in reactjs
What is component in reactjs
 
react redux.pdf
react redux.pdfreact redux.pdf
react redux.pdf
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
 
JDBC
JDBCJDBC
JDBC
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
 
Best Practice-React
Best Practice-ReactBest Practice-React
Best Practice-React
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
React workshop
React workshopReact workshop
React workshop
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
 
WEB DEVELOPMENT USING REACT JS
 WEB DEVELOPMENT USING REACT JS WEB DEVELOPMENT USING REACT JS
WEB DEVELOPMENT USING REACT JS
 
Meetup angular http client
Meetup angular http clientMeetup angular http client
Meetup angular http client
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
react-slides.pptx
react-slides.pptxreact-slides.pptx
react-slides.pptx
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 

Similar to Presentation1.pptx

Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajaxNir Elbaz
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesWebStackAcademy
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP TutorialLorna 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 Experiencereeder29
 
Web Technologies in Java EE 7
Web Technologies in Java EE 7Web Technologies in Java EE 7
Web Technologies in Java EE 7Lukáš 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 DevelopersBrian Huff
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiTiago Knoch
 
Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...
Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...
Леонід Кузьмін “Сам собі паблішер. Від сайту ігрової студії до універсального...Lviv Startup Club
 
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 pownedDinis Cruz
 
Coding 100-session-slides
Coding 100-session-slidesCoding 100-session-slides
Coding 100-session-slidesCisco DevNet
 
WEB TECHNOLOGY Unit-5.pptx
WEB TECHNOLOGY Unit-5.pptxWEB TECHNOLOGY Unit-5.pptx
WEB TECHNOLOGY Unit-5.pptxkarthiksmart21
 

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

Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 

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 );