𝗥𝗲𝗾𝘂𝗲𝘀𝘁 𝗠𝗲𝘁𝗵𝗼𝗱 𝗙𝗶𝗹𝘁𝗲𝗿𝗶𝗻𝗴 𝗶𝗻 𝗛𝘆𝗽𝗲𝗿𝗹𝗮𝗻𝗲

Web applications handle different HTTP methods like GET, POST, and DELETE. Hyperlane gives you several ways to filter these requests. You can route requests to specific handlers or use middleware to check methods.

Here are the ways to filter methods in Hyperlane:

  • Use #[is_get_method] for simple read-only endpoints. This macro rejects any request that is not a GET request.

  • Use #[methods] for flexible routing. You can pass a list of allowed methods. For example, #[methods("GET", "POST")] allows both reading and creating data.

  • Use manual checks for complex logic. You can pull the method from the context and use if-else statements to decide how to respond.

  • Use #[filter] for route-level control. You can combine this with route definitions to create specific handlers for different methods on the same path.

  • Use middleware for logging or debugging. You can extract the method and add it to your request attributes.

Best practices for your API:

  • Use #[is_get_method] for read-only tasks to keep code clean.
  • Use #[methods] when an endpoint needs more than one type of request.
  • Return a 405 status code if a user sends an unsupported method.
  • Combine filters to create strict rules. You can check the method, the host, and the path length all at once.

These tools help you build clean and organized APIs. They make your code easier to read and maintain.

Project Code: https://github.com/hyperlane-dev/hyperlane

Source: https://dev.to/tengxgfyrz67s/request-method-filtering-35jk