Method Override

Harpia supports HTTP method overriding, a technique used to simulate HTTP verbs like PUT, PATCH, or DELETE in environments that only support standard GET and POST requests, such as HTML forms.

How it Works

The method override middleware checks for a _method field within the body of a POST request. If found, and if the request’s Content-Type is application/x-www-form-urlencoded, Harpia will treat the request as the specified HTTP verb during routing and middleware execution.

Usage in HTML Forms

To use method overriding in an HTML form, set the method attribute to POST and include a hidden input field named _method with the desired HTTP verb.

<!-- Example: A form to delete a user -->
<form action="/users/1" method="POST">
  <input type="hidden" name="_method" value="DELETE" />
  
  <p>Are you sure you want to delete this user?</p>
  <button type="submit">Delete User</button>
</form>

When this form is submitted:

  1. The browser sends a POST request to /users/1.
  2. Harpia intercepts the request and reads the _method field.
  3. The request is routed to your app.delete("/users/:id", ...) handler instead of a POST handler.

Supported Verbs

You can override the POST method with any of the following HTTP verbs:

  • GET
  • PUT
  • PATCH
  • DELETE
  • HEAD
  • OPTIONS

Requirements

For method override to work, the original HTTP request method must be POST and the Content-Type must be application/x-www-form-urlencoded. The framework will ignore the _method field if the request does not meet these criteria.

Why use Method Override?

While modern APIs typically use the correct HTTP verbs directly, many web browsers still only support GET and POST for standard HTML form submissions. Method overriding allows you to build RESTful applications that work seamlessly with traditional HTML forms while maintaining a clean and semantic API structure.