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

如何在Playwright Java中提取带请求体与URL的完整请求

如何打印Playwright中包含请求体的完整POST请求

我用Playwright发送带请求体的POST请求时,打印requestContext得到的是com.microsoft.playwright.impl.APIRequestContextImpl@771a660这样的输出,没法看到完整请求内容,希望能打印包含请求体的完整请求用于调试。我的代码如下:

import com.microsoft.playwright.APIRequest;
import com.microsoft.playwright.APIRequestContext;
import com.microsoft.playwright.APIResponse;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.RequestOptions;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;

public class PostJSONObjectSimple {
    Playwright playwright;
    APIRequest request;
    APIRequestContext requestContext;

    @BeforeClass
    public void setup() {
        playwright = Playwright.create();
        request = playwright.request();
        requestContext = request.newContext();

    }

    @Test
    public void createUserTest() throws IOException {

        // 创建存储请求体的JSON对象
        JSONObject payloadJson = new JSONObject();

        // 向JSON对象添加字段
        payloadJson.put("name", "John Doe");
        payloadJson.put("job", "IT Manager");

        APIResponse apiPostResponse = requestContext.post("https://reqres.in/api/users",
                RequestOptions.create()
                        .setHeader("Content-Type", "application/json")
                        .setData(payloadJson)
        );
        // 校验响应状态码
        Assert.assertEquals(apiPostResponse.status(), 201);
        System.out.println(requestContext.toString());
    }
}

解决方案

1. 直接打印构造的请求体

你已经手动构造了payloadJson对象,直接打印它就能看到请求体内容:

// 发送请求前后都可以打印
System.out.println("请求体: " + payloadJson.toString(4)); // 带4空格缩进,格式更清晰

2. 通过请求监听捕获完整请求详情

在创建requestContext后添加请求监听器,能自动捕获所有通过该上下文发送的请求的完整信息(URL、请求方法、头、请求体):

@BeforeClass
public void setup() {
    playwright = Playwright.create();
    request = playwright.request();
    requestContext = request.newContext();
    
    // 添加请求监听逻辑
    requestContext.onRequest(request -> {
        System.out.println("\n=== 完整请求信息 ===");
        System.out.println("请求URL: " + request.url());
        System.out.println("请求方法: " + request.method());
        System.out.println("请求头: " + request.headers());
        // 打印请求体(仅POST/PUT等带体的请求有值)
        request.postData().ifPresent(body -> System.out.println("请求体: " + body));
    });
}

3. 通过APIResponse获取对应请求信息

从返回的apiPostResponse对象反向获取请求实例,再打印详情:

// 在断言之后添加
System.out.println("\n=== 请求详情 ===");
System.out.println("请求URL: " + apiPostResponse.request().url());
System.out.println("请求方法: " + apiPostResponse.request().method());
System.out.println("请求头: " + apiPostResponse.request().headers());
apiPostResponse.request().postData().ifPresent(body -> System.out.println("请求体: " + body));

为什么原代码打印结果不符合预期

APIRequestContext是一个请求上下文对象,它的默认toString()方法只会输出类的全限定名和内存哈希码,不会包含任何请求的具体内容,所以需要通过上述方法获取实际的请求信息。

内容的提问来源于stack exchange,提问作者Sameera De Silva

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 23:55:06