我想发送一个 POST 请求,其中 -
在放心代码中,我可以使用以下代码获取字符串格式的静态 JSON 有效负载 -
String jsonFilePath = "<<Path to JSON File>>/Test_new.json";
String response = given().log().all().header("X-AUTH-TOKEN",res).body(new String(Files.readAllBytes(Paths.get(jsonFilePath)))).
when().post("<<POST RESOURCE URL>>").
then().log().body().assertThat().statusCode(200).extract().response().asString();
Run Code Online (Sandbox Code Playgroud)
运行此代码时,仅使用静态 JSON 有效负载,我收到“415”错误代码。 问题 -
您需要使用multiPart()方法来上传文件,而不是body()方法。例如:
File json = new File("src/test/resources/test_new.json");
File file = new File("src/test/resources/debug.log");
given().log().all()
.multiPart("files", file)
.multiPart("body", json, "application/json")
.post("your_url");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3107 次 |
| 最近记录: |