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

URL中等号如何转义?RestAssured测试queryParam带等号编码问题怎么解决

解决方案

你当前的写法误用了queryParam方法,该方法第一个参数为查询参数名,第二个为参数值,框架会自动拼接=,不需要手动在参数名里加等号。

最简洁修复方式

直接将参数名和参数值分开传入queryParam即可:

String customerId = "1234";

given().queryParam("customerId", customerId)
        .spec(customerApi).auth().oauth2(token)
.when().log().ifValidationFails()
       .get("/api/v1/customers/latest")
.then()
       .assertThat()
       .statusCode(201);

这种写法框架会自动生成合法的查询参数customerId=1234,不会出现等号被编码的问题。


如果你确实有特殊需求,需要在查询参数的键或值中保留原生=不被编码,可以手动关闭URL编码开关:

given().urlEncodingEnabled(false)
        .queryParam("customerId=" + customerId)
        // 其余逻辑不变

注意关闭URL编码后,所有特殊字符都不会被自动转义,你需要自行处理参数中其他需要编码的字符,避免请求异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 12:36:03