Lesson Tuesday

HTTP Requests

In the applications we've built so far, we've used the following HTTP methods:

  • GET retrieves information from the server. We use GET requests to retrieve information from our database. Then, we usually display that information back to our user. As you know, Spark also includes a corresponding get() method. We use this method to create routes that are executed upon an HTTP GET request. In Spark, HTML forms without a specified method attribute of post are sent as GET requests by default.

  • POST alters information on the server. Spark also has a corresponding post() method to create routes that are triggered upon an HTTP POST request. We must add a method attribute of post to an HTML form for its submission to trigger a POST request. So far, we've used POST requests when we're creating new instances of a class in our applications.

However, to update or destroy information (the "U" and "D" in "CRUD") the HTTP specification often uses two other HTTP methods: PATCH (or PUT) and DELETE. We'll learn about updating information in this lesson, and deleting information in the next.

PATCH Requests

The PATCH method updates the server. It's like a more-specific POST request. POST changes information on the server, often adding entirely new information. On the other hand, PATCH updates existing information.

For instance, when you update the caption of a photo you previously posted on Facebook, you're submitting a PATCH request. The photo already exists on their server, you're just updating one of its attributes: The caption.

When an application persists data in a database, the corresponding SQL command to update that information's entry in the database looks like something this:

"UPDATE table_name SET column_name = :variable_containing_new_info WHERE id = :id_of_object;"


Remember, browsers request pages by making a request for a URL using a specific HTTP method, such as GET, POST, PATCH and DELETE. Each method is a request to perform an operation on a resource.

In modern web applications, we typically use the same URL and different HTTP methods to convey what we're trying to do. Using the same URL patterns with different HTTP methods or actions is part of a widely-accepted approach for designing web applications called REST, or REpresentational State Transfer. We've been creating RESTful routes in our applications since last week. You may want to revisit the lesson on REST at this point to review if this is feeling unclear.

For reference, here's a table listing all of the CRUD functionalities, the corresponding SQL keywords to execute each functionality in the database, and the HTTP request often associated with each action:


Limitations of HTML Forms

Now, it's important to know about all HTTP requests: GET, POST, PATCH and DELETE. However, it's also important to know that HTML forms actually have limitations in the type of requests they can trigger.

We've used forms to execute both GET and POST requests already. Therefore, one might assume that forms will trigger our PATCH requests to update an object, and DELETE requests to delete one too.

Unfortunately this is not true. HTML forms can only trigger GET and POST requests. At one point, early drafts of HTML5 did support different requests in HTML forms. If you're curious, you can see these drafts from W3 here. W3 is the World Wide Web Consortium. They develop standards for HTML and the web as a whole.

However, support was later removed before HTML5 was finalized and released. You don't need to know these details or politics though; simply know that HTML forms may only trigger GET or POST requests. Perhaps in the future they'll execute PATCH and UPDATE requests too, but for now they cannot.

Working Around HTML Limitations in Spark

And, go figure, Spark does contain a method called put() to create routes for PATCH requests. And a delete() method to define routes to handle DELETE requests, as seen in its documentation.

However, as discussed above, we cannot trigger these requests using HTML forms. These methods are included because Spark is often used to create APIs, where other websites and programs execute requests to retrieve data from a Spark application. This is done without a form, so they do not encounter the same limitations. You'll learn a lot more about APIs in your upcoming JavaScript course.

Overall, just know that HTML forms can (currently) only execute GET and POST requests. Due to this, we must use POST requests to update and delete records. Be aware that PATCH and DELETE HTTP requests exist but cannot be triggered through forms.