DEV Community

Kelvin Kariuki
Kelvin Kariuki

Posted on

Developer Take on: RFC 10008 - The New HTTP Query Method

Developer Take on: RFC 10008 - The New HTTP Query Method

As developers, we're accustomed to working with the familiar HTTP request methods: GET, POST, PUT, and DELETE. However, a new contender has emerged, changing the game for HTTP interaction: RFC 10008 Introduce the Query Method. In this article, we'll delve into what it means for developers and explore how to integrate this new query method into our applications.

The Birth of a New Standard

The introduction of a new query method might raise questions: "Is this just another feature in an ever-growing list of HTTP extensions?" In reality, the new query method offers more than just a new way to do things. It fills a long-standing gap within the HTTP framework. The new RFC has a potential to improve the HTTP specification that has grown around the existing query methods over the years.

Why RFC 10008 Matters

The current query methods are somewhat limited in their design. When you want to query data, you often have to resort to POST requests (for example, when sending an authenticated query). The primary use case of POST is not exactly well-suited for queries; its design focuses more on the creation or update of resources.

The new query method addresses this issue by introducing a more purposeful request method for retrieval of information from the server. With this addition to the HTTP protocol, we should be able to achieve what we want: a clean way to fetch information.

How It Works

The new query method has two significant characteristics. Firstly, it introduces a new, more specific, query method to the HTTP protocol. This query method should be as straightforward to use and to implement. Secondly, the RFC also updates the rules around HTTP request headers, providing more flexibility when using queries.

Request and Response Structure

In HTTP, the request method determines how the client and server interact. RFC 10008 introduces the new method using the following structure: HTTP/1.1 200 OK.

For example, a request for fetching the first few elements in a collection of items could be: GET /items?limit=10 with the response being an array of up to ten items in JSON format. Now with RFC 10008, you could send a single HTTP request and have a response similar to this: [item1, item2, item3, ... item10]. A key difference is the server can decide between returning a list or individual items in response to your query.

However, the server has the option of returning items individually. For example, if you use the query method /items?limit=10, the response can be a series of single item, which would be similar to this:

{
    "items": [
        {
            "id": 1,
            "name": "Item 1"
        },
        {
            "id": 2,
            "name": "Item 2"
        }
        // ... for a total of 10 items
    ]
}
Enter fullscreen mode Exit fullscreen mode

As demonstrated, we can now achieve more flexible HTTP requests, improving how data retrieval happens within our applications.

Practical Use with Railway

We'll now see how Railway can use the new query method.

With Railway, we can integrate our new method in a very straightforward manner. When it comes to our API endpoints, we're able to leverage Railway's API Gateway capabilities easily, and we can use this integration to handle this new request structure.

In this example, let's create an endpoint to get a list of all our users with a limit of items we're returning:

const { requestHandler } = require("@trainrail/railway");

exports.handler = requestHandler(({ params }) => {
  const users = params.limit
    ? await apiGateway.get("/users", { params: { limit: params.limit } })
    : await apiGateway.get("/users") ;

  return {
    statusCode: 200,
    body: JSON.stringify(users),
  };
});
Enter fullscreen mode Exit fullscreen mode

This example leverages the Railway API Gateway to fetch a list of users that match our specified query, allowing for both the new query structure to return a flexible response and for the user to decide how data should be returned.

Conclusion

RFC 10008 has introduced a new HTTP query method that is set to change how servers and clients interact. With its more straightforward, flexible structure, it provides an opportunity for cleaner, more efficient code.

As a developer, this update should be at the forefront of new HTTP query interactions.

Resources

If you'd like to explore more on Railway visit https://railway.app/ or find out more about the other tools used in this article here:
https://tinyurl.com/2xvv7zum (Railway)
https://tinyurl.com/2c5spxjz (Hostinger)

Tags: rfc, http, query-method, api-design, railway

Top comments (0)