RestAssured - 提供正文的内容类型作为不带引号的原始文本

问题描述 投票:0回答:1

我需要将原始文本作为正文传递,不带引号,放心

使用的示例代码:

String command = "dumpsys package com.android.chrome | grep versionName"
request.pathParam("deviceId", deviceId);
request.contentType(ContentType.TEXT).body(command);

但它仍然将其读取为带引号的字符串,但在这里我需要传递正文,如下所示

dumpsys package com.android.chrome | grep versionName

但它被认为是

"dumpsys package com.android.chrome | grep versionName"

因此我的案例失败了

java curl rest-assured
1个回答
0
投票

您的正文发送时不带引号。您可以尝试使用

netcat
工具。

开始监听某个端口(例如

9123
):
netcat -l -p 9123

然后运行代码:

String command = "dumpsys package com.android.chrome | grep versionName";
RestAssured
  .given()
    .baseUri("http://0.0.0.0:9123")
    .basePath("/{deviceId}")
    .pathParam("deviceId", 123)
    .contentType(ContentType.TEXT)
    .body(command)
  .get();

并观看

netcat
的日志:

alexey@master-host:~$ netcat -l -p 9123
GET /123 HTTP/1.1
Accept: */*
Content-Type: text/plain; charset=ISO-8859-1
Content-Length: 53
Host: 0.0.0.0:9123
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.13 (Java/11.0.20.1)
Accept-Encoding: gzip,deflate

dumpsys package com.android.chrome | grep versionName
© www.soinside.com 2019 - 2024. All rights reserved.