AXIOS REACT JS
πππππππππππππππππππππππ
axios({
method: 'post',
url: 'https://api.example.com/data',
data: {
// Your data object here
},
headers: {
'Content-Type': 'application/json', // For sending JSON data
'Accept': 'application/json', // For specifying what response types are acceptable
'Content-Type': 'application/x-www-form-urlencoded', // For sending form data
'Content-Type': 'multipart/form-data', // For sending multipart form data (like files)
'Content-Type': 'text/plain', // For sending plain text data
'Content-Type': 'application/xml', // For sending XML data
'Content-Type': 'application/octet-stream' // For sending binary data
// Add more headers as needed
}
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Error:', error);
});
method: 'post',
url: 'https://api.example.com/data',
data: {
// Your data object here
},
headers: {
'Content-Type': 'application/json', // For sending JSON data
'Accept': 'application/json', // For specifying what response types are acceptable
'Content-Type': 'application/x-www-form-urlencoded', // For sending form data
'Content-Type': 'multipart/form-data', // For sending multipart form data (like files)
'Content-Type': 'text/plain', // For sending plain text data
'Content-Type': 'application/xml', // For sending XML data
'Content-Type': 'application/octet-stream' // For sending binary data
// Add more headers as needed
}
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Error:', error);
});
πππππππππππππππππππππππ
axios({
method: 'get',
url: 'https://api.example.com/data',
headers: {
'Content-Type': 'application/json', // Specifies JSON data
'Accept': 'application/json', // Specifies acceptable response types
'Authorization': 'Bearer <token>', // Sends authentication token
'X-Custom-Header': 'value', // Example of a custom header
'Cache-Control': 'no-cache', // Controls caching behavior
'User-Agent': 'MyApp/1.0.0', // Identifies the client software
'Cookie': 'name=value; name2=value2', // Sends cookies to the server
'Referer': 'https://example.com/page1', // Specifies referring URL
'Origin': 'https://example.com' // Specifies the origin in CORS scenarios
// Add more headers as needed
}
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Error:', error);
});
πππππππππππππππππππππππ
AXIOS response :-
response.status
response.statusText
response.headers
response.data
error.response.status
error.response.statusText
error.response.data
error.request
error.message
success:
response.statusText π
Ok,Created,Accepted,No Content, Moved Permanently, Found, Bad Request, Unauthorized, Forbidden, Not Found, Method Not Allowed, Too Many Request
Errors:
error.response.statusTextπ
Internal Server Error, Bad Gateway, Service Unavailable, Gateway Timeout
examples π
.catch(error=>{
if (error.response.statustext === "Unauthorized"){
navigate("/login");
}
})
axios.get(`http://localhost:8000/get_id/${id}/`,{
headers: {
'Authorization': `Bearer ${sessionStorage.getItem('access_token')}`, // Send token in Authorization header
}})
.then(response=>{
setCity(response.data.city);
setState(response.data.state);
setCountry(response.data.country);
})
.catch(error=>console.error(error))
Comments
Post a Comment