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

Rest Assured调用API报错java.lang.IllegalStateException:不支持application/octet-stream

解决RestAssured验证application/octet-stream响应时的解析错误

当你使用RestAssured验证返回application/octet-stream类型的Web服务响应时,会触发如下错误:

java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but content-type 'application/octet-stream' is not supported out of the box.
Try registering a custom parser using:
RestAssured.registerParser("application/octet-stream", );

解决方案

结合你提到的响应实际为XML格式的情况,可按以下方式处理:

  • 全局注册解析器
    在测试初始化阶段(比如带@BeforeClass注解的方法)注册XML解析器,后续所有请求都能自动识别该Content-Type:

    @BeforeClass
    public static void setup() {
        RestAssured.registerParser("application/octet-stream", Parser.XML);
    }
    

    如果响应是JSON格式,替换为Parser.JSON即可。

  • 单个请求指定解析器
    若不想全局生效,可针对单个请求临时指定解析器:

    given()
        .when()
            .get("/target-api-path")
        .then()
            .statusCode(200)
            .using()
                .parser("application/octet-stream", Parser.XML)
            .body("xml节点路径", equalTo("预期值"));
    
  • 响应验证示例
    注册解析器后,就能正常使用RestAssured的断言方法验证XML响应内容,比如验证某个节点的取值:

    given()
        .when()
            .get("/your-service-endpoint")
        .then()
            .body("root.node.child", equalTo("expected-content"));
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 20:05:58