How can the HTTP QUERY method be implemented in ASP.NET Core, Node.js, and Spring Boot?
How can the HTTP QUERY method be implemented in ASP.NET Core, Node.js, and Spring Boot?
1 Answer
The
HTTP QUERY method is a proposed HTTP method designed for safe, idempotent requests that include a request body. It addresses a long-standing limitation of
GET, which technically does not define semantics for request bodies and is inconsistently supported by clients, proxies, and servers.
Unlike GET, QUERY allows clients to send complex filtering criteria in the request body while maintaining safe and cacheable semantics.
Below are example implementations in ASP.NET Core, Node.js (Express), and Spring Boot.
1. ASP.NET Core
ASP.NET Core doesn't natively include a [HttpQuery] attribute, but you can map the HTTP method manually.
Controller
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("products")]
public class ProductsController : ControllerBase
{
// Maps requests using the HTTP QUERY method.
[AcceptVerbs("QUERY")]
public IActionResult QueryProducts([FromBody] ProductFilter filter)
{
return Ok(new
{
Message = "QUERY request received",
Filter = filter
});
}
}
public class ProductFilter
{
public string Category { get; set; }
public decimal MinPrice { get; set; }
public decimal MaxPrice { get; set; }
}
Example Request
QUERY /products HTTP/1.1
Host: localhost:5000
Content-Type: application/json
{
"category":"Laptop",
"minPrice":500,
"maxPrice":1500
}
2. Node.js (Express)
Express allows registering custom HTTP methods by extending the router.
const express = require("express");
const app = express();
// Parse JSON request bodies.
app.use(express.json());
// Register a QUERY handler.
app.all("/products", (req, res, next) => {
// Continue only for QUERY requests.
if (req.method !== "QUERY") {
return next();
}
// Return the received filter.
res.json({
message: "QUERY request received",
filter: req.body
});
});
// Start the server.
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Example request:
QUERY /products
Content-Type: application/json
{
"category":"Books"
}
3. Spring Boot
Spring MVC doesn't provide a dedicated annotation for QUERY, but you can specify the HTTP method using
@RequestMapping.
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/products")
public class ProductController {
// Handle QUERY requests.
@RequestMapping(method = RequestMethod.valueOf("QUERY"))
public ProductResponse queryProducts(@RequestBody ProductFilter filter) {
ProductResponse response = new ProductResponse();
response.setMessage("QUERY request received");
response.setFilter(filter);
return response;
}
}
ProductFilter
public class ProductFilter {
private String category;
private double minPrice;
private double maxPrice;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public double getMinPrice() {
return minPrice;
}
public void setMinPrice(double minPrice) {
this.minPrice = minPrice;
}
public double getMaxPrice() {
return maxPrice;
}
public void setMaxPrice(double maxPrice) {
this.maxPrice = maxPrice;
}
}
Testing with curl
Many versions of curl allow specifying arbitrary methods:
curl -X QUERY http://localhost:3000/products \
-H "Content-Type: application/json" \
-d '{
"category":"Laptop",
"minPrice":500,
"maxPrice":1500
}'
Things to Consider
Although these examples show how to route QUERY requests, real-world adoption requires more than application code:
- Client support: Not all HTTP clients expose a convenient way to send
QUERYrequests. - Proxies and load balancers: Some intermediaries only recognize standard methods (
GET,POST,PUT, etc.) and may reject or mishandle unknown methods. - API gateways: You may need to explicitly configure gateways (such as NGINX, Apache, or cloud API gateways) to allow the
QUERYmethod. - Caching: While
QUERYis intended to be safe and potentially cacheable, cache implementations may not yet support it consistently. - Framework compatibility: Routing can usually be configured as shown above, but middleware, security filters, and tooling may require additional configuration.
As of today, the QUERY method is still an emerging HTTP feature rather than a universally supported standard, so many production APIs continue to use
POST for complex queries when broad compatibility is required.