How to make PUT request using XMLHttpRequest by making Custom HTTP library ? Last Updated : 28 Nov, 2022 Comments Improve Suggest changes Like Article Like Report The task is to show how the XMLHttpRequest can be used to PUT/Update data to an API by making custom HTTP library. We will be taking a fake API which will contain Array of objects as an example and from that API we will show to PUT data by XMLHttpRequest method by making a custom HTTP library.Used API: https://jsonplaceholder.typicode.com/posts/5What is Ajax? Asynchronous JavaScript and XML, is used to communicate with the server without refreshing the web page and thus increasing the user experience and better performance. To read more about Ajax click on https://www.geeksforgeeks.org/ajax-introduction/.Prerequisites: Only the basic knowledge of HTML, CSS, and JavaScript is required.Note: First make a HTML file and add the HTML markup according to the requirement. In the bottom of the body tag attach two scripts file as library.js and app.js in the same order.Steps required to make library.js File: library.js file make a function easyHTTP to initialize a new XMLHttpRequest() method.Set easyHTTP.prototype.put to a function which contains three parameters 'url', data and callback.Now open an object using this.http.open function. It takes three parameters, the first one is request type (GET or POST or PUT or DELETE), second is the URL for the API and last one is a boolean value (true means asynchronous call and false means synchronous call).Now we will use onload function to display the data. But before that first we need to set content-type with this.http.setRequestHeader method and also assign this keyword to self to have scope of this keyword into onload function. The onload function is executed after the API call is done. This function will run a callback function which has two arguments as error and response text.Last step is to send the request using the send() function. It should be noted here that send() function needs to send data after converting object data to string using JSON.stringify(data). Steps required to make app.js File: First of all instantiate easyHTTP with new keyword.Create a custom data (object) to put/update data.Pass URL, data and a callback function in put prototype function.The callback function contains two arguments error to print if any error occurs and response to get the actual response. Filename: index.html html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>Put request</title> </head> <body> <h1> Put request using xmlhttpRequest/Ajax by making custom HTTP library. </h1> <div class="result"></div> <!-- Including library.js and app.js files --> <script src="library.js"></script> <script src="app.js"></script> </body> </html> Filename: library.js javascript function easyHTTP() { // Initializing new XMLHttpRequest method. this.http = new XMLHttpRequest(); } // Make an HTTP PUT Request easyHTTP.prototype.put = function(url, data, callback) { // Open an object (POST, PATH, ASYNC-TRUE/FALSE) this.http.open('PUT', url, true); // Set content-type this.http.setRequestHeader( 'Content-type', 'application/json'); // Assigning this to self to have // scope of this into the function onload let self = this; // When response is ready this.http.onload = function() { // Callback function (Error, response text) callback(null, self.http.responseText); } // Since the data is an object so // we need to stringify it this.http.send(JSON.stringify(data)); } Filename: app.js javascript // Instantiating easyHTTP const http = new easyHTTP; // Data that we need to update const data = { title: 'Custom Putt', body: 'This is a custom put' }; // Put prototype method(url, data, // response text) http.put( 'https://jsonplaceholder.typicode.com/posts/5', data, function(err, post){ if(err) { console.log(err); } else { console.log(post); } }); Output: Open your index.html file in any browser and open its console by right click->Inspect element->console. Hence you will see the below result. Comment More infoAdvertise with us Next Article How to make PUT request using XMLHttpRequest by making Custom HTTP library ? T thacker_shahid Follow Improve Article Tags : JavaScript Web Technologies HTML Node.js JavaScript-Misc Node.js-Misc +2 More Similar Reads DELETE request using XMLHttpRequest by making Custom HTTP library The task here is to show how the XMLHttpRequest can be used to DELETE data to an API by making a custom HTTP library. A placeholder API that contains an array of objects would be used as an example. The DELETE request is performed on this API. The URL of the API is https://jsonplaceholder.typicode.c 3 min read How to make simple PUT request using fetch API by making custom HTTP library ? The fetch() method is used to send the requests to the server without refreshing the page. It is an alternative to the XMLHttpRequest object. It will be taking a fake API that will contain Array as an example and from that API we will show to PUT/Update  data by  fetch API  method by making custom H 2 min read POST request using AJAX by making Custom HTTP library The task is to show how the XMLHttpRequest can be used to POST data to an API by making custom HTTP library. We will be taking a fake API which will contain Array of objects as an example and from that API we will show to POST data by XMLHttpRequest method by making a custom HTTP library. Used API: 4 min read Get request using AJAX by making Custom HTTP library The task is to show how the XMLHttpRequest can be used to get data from an API by making custom HTTP library. I will be taking a fake API that will contain an array of objects as an example and from that API, we will show to get data by XMLHttpRequest method by making a custom HTTP library. API link 3 min read Simple DELETE request using fetch API by making custom HTTP library Why fetch() API method is used? The fetch() method is used to send the requests to the server without refreshing the page. It is an alternative to the XMLHttpRequest object. We will be taking a fake API which will contain Array as an example and from that API we will show to DELETE data by fetch API 2 min read GET and POST Requests using Fetch API with Custom HTTP Library This tutorial covers the basics of using the Fetch API to perform GET and POST requests within a custom HTTP library. Wiasynchronous operations and handling JSON data, you can effectively fetch data from remote APIs and submit data to servers. This approach gives you greater control and flexibility 3 min read How to make HTTP requests in Node ? In the world of REST API, making HTTP requests is the core functionality of modern technology. Many developers learn it when they land in a new environment. Various open-source libraries including NodeJS built-in HTTP and HTTPS modules can be used to make network requests from NodeJS.There are many 4 min read How to Intercept HTTP requests in web extension ? In this article, we will understand to intercept the HTTP requests made by the browser using web extension API.What is Intercepting HTTP Request?Intercepting HTTP Requests means manipulating the HTTP requests made by the browser through the user or through asynchronous event handlers. With the help 3 min read How Are Parameters Sent In An HTTP POST Request? HTTP POST requests are widely used in web development to send data from a client to a server. Whether you're submitting a form, uploading a file, or sending JSON data via an API, understanding how parameters are sent in an HTTP POST request is important. In this article, weâll explore how are parame 4 min read What are the uses of XMLHTTPRequest Object in Ajax ? In this article, we will know about the XMLHTTPRequest and how it can be useful for client-server request and properties of XMLHttpRequest.XMLHTTPRequest is an object that sends an HTTP request to the server and interacts with the server to open a URL and retrieve data without loading the complete p 3 min read Like