HTTP Requests in JavaScript : Techniques Every Developer Should Know in 2024

POST: Send New Data to a Server

PUT: Update Existing Data on a Server

fetch('https://api.mocki.io/v1/b043df5a')  
  .then(response => response.json()) // Converts the response to JSON  
  .then(data => console.log(data))    // Logs the data to the console  
  .catch(error => console.error('Error fetching data:', error)); // Handles errors  

Fetch API: POST Request

fetch('https://api.mocki.io/v1/b043df5a', {  
  method: 'POST', // Specifies the request method as POST  
  body: JSON.stringify({  
    name: 'John Doe',  
    email: 'john.doe@example.com'  
  }), // Converts the data to a JSON string  
  headers: {  
    'Content-Type': 'application/json' // Sets the content type to JSON  
  }  
})  
  .then(response => response.json()) // Converts the response to JSON  
  .then(data => console.log(data))    // Logs the data to the console  
  .catch(error => console.error('Error posting data:', error)); // Handles errors  
fetch('https://api.mocki.io/v1/b043df5a/1', {  
  method: 'PUT', // Specifies the request method as PUT  
  body: JSON.stringify({  
    name: 'Jane Doe',  
    email: 'jane.doe@example.com'  
  }), // Converts the data to a JSON string  
  headers: {  
    'Content-Type': 'application/json' // Sets the content type to JSON  
  }  
})  
  .then(response => response.json()) // Converts the response to JSON  
  .then(data => console.log(data))    // Logs the data to the console  
  .catch(error => console.error('Error updating data:', error)); // Handles errors  
fetch('https://api.mocki.io/v1/b043df5a/1', {  
  method: 'DELETE' // Specifies the request method as DELETE  
})  
  .then(() => console.log('Data deleted successfully')) // Logs success message  
  .catch(error => console.error('Error deleting data:', error)); // Handles errors  
const getData = (url, callback, errorCallback) => {  
  const xhr = new XMLHttpRequest();  
  xhr.open('GET', url, true); // Initializes a GET request  
  xhr.onload = () => callback(xhr.responseText); // Handles a successful response  
  xhr.onerror = () => errorCallback(xhr); // Handles errors  
  xhr.send(); // Sends the request  
};  
  
getData('https://api.mocki.io/v1/b043df5a', console.log, console.error);  

XMLHttpRequest: POST Request

const postData = (url, data, callback, errorCallback) => {  
  const xhr = new XMLHttpRequest();  
  xhr.open('POST', url, true); // Initializes a POST request  
  xhr.setRequestHeader('Content-Type', 'application/json'); // Sets the content type to JSON  
  xhr.onload = () => callback(xhr.responseText); // Handles a successful response  
  xhr.onerror = () => errorCallback(xhr); // Handles errors  
  xhr.send(JSON.stringify(data)); // Converts the data to a JSON string and sends it  
};  
  
postData('https://api.mocki.io/v1/b043df5a', {  
  name: 'John Doe',  
  email: 'john.doe@example.com'  
}, console.log, console.error);  
const updateData = (url, data, callback, errorCallback) => {  
  const xhr = new XMLHttpRequest();  
  xhr.open('PUT', url, true); // Initializes a PUT request  
  xhr.setRequestHeader('Content-Type', 'application/json'); // Sets the content type to JSON  
  xhr.onload = () => callback(xhr.responseText); // Handles a successful response  
  xhr.onerror = () => errorCallback(xhr); // Handles errors  
  xhr.send(JSON.stringify(data)); // Converts the data to a JSON string and sends it  
};  
  
updateData('https://api.mocki.io/v1/b043df5a/1', {  
  name: 'Jane Doe',  
  email: 'jane.doe@example.com'  
}, console.log, console.error);  
const deleteData = (url, callback, errorCallback) => {  
  const xhr = new XMLHttpRequest();  
  xhr.open('DELETE', url, true); // Initializes a DELETE request  
  xhr.onload = () => callback(xhr.responseText); // Handles a successful response  
  xhr.onerror = () => errorCallback(xhr); // Handles errors  
  xhr.send(); // Sends the request  
};  
  
deleteData('https://api.mocki.io/v1/b043df5a/1', () => console.log('Data deleted successfully'), console.error);  

Conclusion

Leave a Reply

Your email address will not be published. Required fields are marked *

×