Api
Api
1. Fetch API
The Fetch API is a built-in JavaScript function that provides a simple way to make
network requests. It’s part of the browser's native JavaScript and does not require
any additional libraries.
Basic Example:
fetch('https://api.example.com/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ key: 'value' }), // Convert data to
JSON
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
2. Axios
Axios is a third-party library that provides a more powerful and feature-rich way to
handle HTTP requests. It offers additional features like request and response
Api 1
interceptors and automatic JSON data transformation.
Installation:
Basic Example:
Comparison:
Error Handling: Fetch does not reject the promise on HTTP error status codes
(e.g., 404 or 500). You need to check response.ok manually. Axios
automatically rejects the promise for HTTP error statuses.
Api 2
Browser Support: Fetch is supported in most modern browsers. Axios has
broader compatibility and supports older browsers as well.
Api 3