Please wait

HTTP Methods

HTTP methods, also known as "request methods", are actions that can be taken on a resource in a web server. They are used in HTTP requests to indicate the desired action to be performed on the specified resource. Think of them as verbs that tell the server what to do.

Here are the two most common HTTP methods:

  • GET: This method is used to retrieve data from the server. For example, when you type a URL into your web browser, your browser sends a GET request to the server to fetch the webpage. The GET method should only retrieve data and should have no other effect on the data.
  • POST: This method is used to send data to the server. For example, when you fill out a web form and click submit, your browser might send a POST request to the server with the data you entered. This may result in a change on the server, such as adding a new record to a database.

Sending Data with GET Methods

When using the HTTP GET method, data is appended to the URL in the form of a query string. This is the part of the URL that comes after a question mark ?.

For example, if you're trying to search for books on a website, your browser might send a GET request like this: http://www.example.com/search?query=books. Here, query=books is the data sent with the GET request.

In the query string, different pieces of data are separated by & symbols. If you wanted to search for books in a specific language, the GET request might look like this: http://www.example.com/search?query=books&language=english.

Here are a few important things to note about GET requests:

  • Visible data: Because data is appended to the URL, it's visible in the browser's address bar. This means that sensitive data, like passwords, should never be sent using GET requests.
  • Bookmarking: Because the data is part of the URL, users can bookmark the exact state of a web page. For example, they could bookmark a search results page.
  • Length restrictions: URLs have a maximum length. This varies between browsers and servers, but in general, you should avoid sending large amounts of data with GET requests.
  • Caching: GET requests can be cached by browsers, meaning they can save a copy of the full URL and its corresponding response. This can make repeated requests faster, but it also means you need to be careful about how you use GET requests.

Sending Data with POST Methods

When using the HTTP POST method, data is sent in the body of the HTTP request, not in the URL as with the GET method.

When you fill out a form on a website and click submit, your web browser might use a POST request to send the data to the server. The data from the form fields are packaged into the body of the HTTP request.

Here's an example of a form that sends a POST request.

<form method="POST">
  <input type="text" placeholder="enter username" name="username" />
  <input type="password" placeholder="enter password" name="password" />
</form>

Browsers automatically format form data in a POST request into the body, which might look like this:

username=test&password=abc123

In this case, the data username=test&password=abc123 is sent in the body of the request.

Here are some important things to note about POST requests:

  • Hidden data: Because the data is in the body of the request, it's not visible in the browser's address bar. This makes POST requests a better choice than GET for sending sensitive data.
  • No length restrictions: POST requests don't have the length restrictions that GET requests do, so they can send larger amounts of data.
  • No bookmarking or caching: Because the data isn't part of the URL, POST requests can't be bookmarked or cached as GET requests can.

Remember, POST requests should be used when you want to send data to the server that changes the server's state in some way. For example, you could use a POST request to add a new record to a database, authenticate a user, or upload a file.

Other HTTP Methods

Even though this lesson focused on GET and POST, there are other popular methods:

  • PUT: This method is used to update a resource on the server. It requires you to send the entire updated resource, not just the changes. If the resource does not exist, it can create it.
  • DELETE: As the name suggests, this method is used to delete a resource on the server.
  • PATCH: This method is used to apply partial modifications to a resource on the server. Unlike PUT, it only requires you to send the changes, not the entire updated resource.

Remember, these methods (GET, POST, PUT, DELETE, PATCH) represent a contract of sorts between the client and server. The server expects the client to use these methods in a consistent way, and when used properly, they help make web applications work smoothly and predictably.

For most web development work, the five methods above will be the ones you use and encounter the most.

Key Takeaways

  • HTTP methods are actions used in HTTP requests to indicate the desired action to be performed on the specified resource.
  • The GET method is used to retrieve data from the server. It appends data to the URL in a query string and should only retrieve data without changing anything on the server.
  • GET has length restrictions due to limitations in URL length. POST does not have these restrictions.
  • The POST method is used to send data to the server that changes the server's state in some way. Data is sent in the body of the HTTP request, making it suitable for sending sensitive or large amounts of data.
  • Data sent with GET requests is visible in the browser's address bar, while data sent with POST is in the request body and is not visible in the address bar.
  • GET requests can be bookmarked and cached because they are included in the URL. POST requests cannot be bookmarked or cached.

Comments

Please read this before commenting