You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从RestAssured Response对象中获取请求类型(GET或POST)?

Great question! Let's break down the solutions for getting the HTTP request method (GET/POST/etc.) from a RestAssured Response object—both built-in and custom approaches:

Built-in Solution

RestAssured's Response object has a direct way to retrieve the associated request details. You can access the original RequestSpecification from the response and call getMethod() to get the HTTP method string (like "GET", "POST", "PUT", etc.).

Here's a quick code example:

// Send a sample GET request
Response response = given()
        .when()
        .get("/api/users/1");

// Fetch the request method from the response
String requestMethod = response.getRequestSpecification().getMethod();

// Output will be "GET"
System.out.println("Request method: " + requestMethod);

This works for all standard request types, and it's the simplest approach for most use cases. Just note that if you've modified the RequestSpecification after sending the request (uncommon in typical flows), this might not reflect the exact method sent—but for standard test workflows, it's reliable.

Custom Solutions

If you need more control or want to handle edge cases (like tracking methods across multiple requests), you can use these custom approaches:

1. Manually Attach Request Method as an Attribute

When sending your request, explicitly add the request method as a custom attribute to the Response object:

// Send a POST request and attach the method as an attribute
Response response = given()
        .attribute("requestMethod", "POST")
        .body("{\"name\": \"John\"}")
        .when()
        .post("/api/users");

// Retrieve the custom attribute later
String requestMethod = response.getAttribute("requestMethod");

2. Use a Custom Filter to Auto-Record Request Method

For a more scalable solution (so you don't have to add attributes manually every time), create a custom Filter that automatically injects the request method into every response:

public class RequestMethodTrackerFilter implements Filter {
    @Override
    public Response filter(FilterableRequestSpecification requestSpec, 
                           FilterableResponseSpecification responseSpec, 
                           FilterContext ctx) {
        // Execute the request and get the response
        Response response = ctx.next(requestSpec, responseSpec);
        // Attach the request method to the response attributes
        response.attribute("requestMethod", requestSpec.getMethod());
        return response;
    }
}

Then register this filter when setting up your requests:

// Use the filter for a request
Response response = given()
        .filter(new RequestMethodTrackerFilter())
        .when()
        .delete("/api/users/1");

// Retrieve the method from the response attribute
String requestMethod = response.getAttribute("requestMethod"); // Will be "DELETE"

This filter will automatically track the request method for every response it's applied to, which is great for larger test suites.


内容的提问来源于stack exchange,提问作者Sasitharan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 16:27:34