RestAssured如何传递无引号的原始文本请求体?
RestAssured传递原始文本请求体去引号解决方案
两种直接有效的解决方式
- 用
StringBody包装请求体
显式声明内容为纯文本,避免被RestAssured默认的序列化逻辑添加引号:
import io.restassured.http.StringBody; String command = "dumpsys package com.android.chrome | grep versionName"; request.pathParam("deviceId", deviceId); request.contentType(ContentType.TEXT) .body(new StringBody(command));
- 指定文本类型的对象映射器
通过重载的body方法传入文本专用映射器,跳过JSON序列化步骤:
String command = "dumpsys package com.android.chrome | grep versionName"; request.pathParam("deviceId", deviceId); request.contentType(ContentType.TEXT) .body(command, new TextGsonObjectMapperFactory());
全局配置(可选)
如果项目频繁发送纯文本请求,可以全局配置RestAssured,让TEXT类型请求体直接传递原始内容:
RestAssured.config = RestAssured.config() .objectMapperConfig(new ObjectMapperConfig() .defaultObjectMapperType(ObjectMapperType.GSON) .textObjectMapperFactory((type, charset) -> new Gson()));
配置完成后,再使用contentType(ContentType.TEXT).body(command)就不会自动添加引号了。
内容的提问来源于stack exchange,提问作者user2416212
相关产品推荐
相关产品推荐

