What Is Http?
HTTP is a stateless protocol, which means that the server does not retain any information about the client between requests. This makes it easy to scale, but it also means that it is not well-suited for applications that require long-running connections or real-time communication.
There are several different HTTP methods that can be used in a request, including GET, POST, PUT, DELETE, and others. The method indicates the type of action that the request is intended to perform. For example, a GET request is used to retrieve data from the server, while a POST request is used to send data to the server (such as when submitting a form).
HTTP is an important foundation of the web, and is used by almost all websites and web-based applications.
Section 1: XMLHttpRequest
To make an HTTP request with XMLHttpRequest, you first need to create a new instance of the object:
Next, you need to specify the HTTP method and URL that you want to send the request to. You can do this using the open method:
The first argument is the HTTP method (e.g. GET, POST, PUT, DELETE), and the second argument is the URL.
Once you have set up the request, you can send it using the
You can also pass a payload to the
send method if you are making a POST, PUT, or DELETE request. For example, to send a JSON payload in the body of a POST request, you would do something like this:
To handle the response from the server, you can set an event handler for the
onreadystatechange event. This event is fired every time the
readyState property of the
XMLHttpRequest object changes. When the request is complete, the
readyState will be set to
DONE, and the
responseText property will contain the response from the server.
Here’s an example of how to handle the response using the
To make an HTTP request with
fetch, you simply call the
fetch function and pass it the URL that you want to send the request to:
fetch function returns a promise that resolves with a
Also Read: 5 Advices Beginner Programmer Must Follow