SlideShare a Scribd company logo
3
Most read
6
Most read
9
Most read
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 (20)

PPTX
Introduction to React JS for beginners
Varun Raj
 
PPTX
React + Redux Introduction
Nikolaus Graf
 
PPTX
Introduction to React
Rob Quick
 
PPTX
React js
Oswald Campesato
 
PDF
Spring MVC Framework
Hùng Nguyễn Huy
 
PPTX
Introduction to react_js
MicroPyramid .
 
PDF
Express node js
Yashprit Singh
 
PPTX
React workshop
Imran Sayed
 
PDF
Understanding react hooks
Samundra khatri
 
PDF
React js
Rajesh Kolla
 
PDF
React Context API
NodeXperts
 
PPTX
ReactJS presentation.pptx
DivyanshGupta922023
 
PPTX
Java 11 to 17 : What's new !?
Jérôme Tamborini
 
PPTX
Express js
Manav Prasad
 
PPTX
Intro to React
Justin Reock
 
PPTX
Reactjs
Neha Sharma
 
PDF
Important React Hooks
Knoldus Inc.
 
PDF
How To Manage API Request with AXIOS on a React Native App
Andolasoft Inc
 
PPTX
Angular Data Binding
Jennifer Estrada
 
PPTX
React js programming concept
Tariqul islam
 
Introduction to React JS for beginners
Varun Raj
 
React + Redux Introduction
Nikolaus Graf
 
Introduction to React
Rob Quick
 
Spring MVC Framework
Hùng Nguyễn Huy
 
Introduction to react_js
MicroPyramid .
 
Express node js
Yashprit Singh
 
React workshop
Imran Sayed
 
Understanding react hooks
Samundra khatri
 
React js
Rajesh Kolla
 
React Context API
NodeXperts
 
ReactJS presentation.pptx
DivyanshGupta922023
 
Java 11 to 17 : What's new !?
Jérôme Tamborini
 
Express js
Manav Prasad
 
Intro to React
Justin Reock
 
Reactjs
Neha Sharma
 
Important React Hooks
Knoldus Inc.
 
How To Manage API Request with AXIOS on a React Native App
Andolasoft Inc
 
Angular Data Binding
Jennifer Estrada
 
React js programming concept
Tariqul islam
 

Similar to Presentation1.pptx (20)

PPTX
API & Backend Integration
Elewayte
 
PPTX
AJAX and JSON
Julie Iskander
 
PPT
Web Programming using Asynchronous JavaX
SivanN6
 
PDF
Advanced I/O in browser
Eugene Lazutkin
 
PPTX
Lecture05.pptx
MrVMNair
 
PPT
Ajax
Manav Prasad
 
PPTX
Fetch API Talk
Chiamaka Nwolisa
 
PPT
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
PPTX
From JSX to Deployment: Mastering the React Workflow for Scalable Front-End D...
sriniravir05
 
PPTX
Web technologies-course 10.pptx
Stefan Oprea
 
PPTX
HSHDGDGDHDYDUDUDUDHDHDHSHAHAHSHSBDHDHDHDHDHD
srlegaspi2101
 
PPT
Ajax tutorial by bally chohan
WebVineet
 
DOCX
Copy of ajax tutorial
Abhishek Kesharwani
 
PPT
Ajax
ch samaram
 
PPTX
mern _stack _power _point_ presentation(1)
susmithalanka2
 
PPTX
Web-Engineering-Lec-14 (1) .pptx
iamayesha2526
 
PPTX
Web-Engineering-Lec-14 (1 ).pptx
iamayesha2526
 
API & Backend Integration
Elewayte
 
AJAX and JSON
Julie Iskander
 
Web Programming using Asynchronous JavaX
SivanN6
 
Advanced I/O in browser
Eugene Lazutkin
 
Lecture05.pptx
MrVMNair
 
Fetch API Talk
Chiamaka Nwolisa
 
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
From JSX to Deployment: Mastering the React Workflow for Scalable Front-End D...
sriniravir05
 
Web technologies-course 10.pptx
Stefan Oprea
 
HSHDGDGDHDYDUDUDUDHDHDHSHAHAHSHSBDHDHDHDHDHD
srlegaspi2101
 
Ajax tutorial by bally chohan
WebVineet
 
Copy of ajax tutorial
Abhishek Kesharwani
 
mern _stack _power _point_ presentation(1)
susmithalanka2
 
Web-Engineering-Lec-14 (1) .pptx
iamayesha2526
 
Web-Engineering-Lec-14 (1 ).pptx
iamayesha2526
 
Ad

Recently uploaded (20)

PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Day2 B2 Best.pptx
helenjenefa1
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Hashing Introduction , hash functions and techniques
sailajam21
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Ad

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