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

😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍
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

Popular posts from this blog

JavaScript Typing Animation