java - 如何使用 RestAssured post 方法上传多张图片

标签 java rest rest-assured rest-assured-jsonpath

Postman - 用于上传图像的 Post Request api 详细信息

我正在执行 POST 请求来上传多个图像。下面是 Postman 的屏幕截图,一切正常,我得到了正确的响应。但随后我需要使用 RestAssured Program 来实现相同的功能。

上面是我尝试过的程序的快照,但我没有收到 500 错误,不知道缺少什么,我在这里做错了什么。

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestLogSpecification;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

Class UploadImages_API {

RestAssured.baseURI = "http://azrvqmobfletap2.corp.ryder.com:86";
httpRequest = RestAssured.given().log().all();

 String file1_new1 = System.getProperty("user.dir")+"\\src\\main\\resources\\ImageUpload\\1.jpg";
String file2_new2 = System.getProperty("user.dir")+"\\src\\main\\resources\\ImageUpload\\2.jpg";
String file3_new3 = System.getProperty("user.dir")+"\\src\\main\\resources\\ImageUpload\\3.jpg";

httpRequest.given()
    .formParam("Year", "2008")
    .formParam("make", "Ford")
    .formParam("model", "Cascadia")
    .formParam("angleTokens", "top,left,bottom")
    .formParam("productCode", "485274")
    .multiPart("pictureData","file1_new1")
    .multiPart("pictureData","file2_new2")
    .multiPart("pictureData","file3_new3");

    response = httpRequest.post("/UsedTrucksInventory/api/Image/UploadImage");

    System.out.println(" Response : "+response.asString());

}

最佳答案

我找到了问题的答案。下面是代码

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestLogSpecification;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

Class UploadImages_API {

RestAssured.baseURI = "http://azrvqmobfletap2.corp.ryder.com:86";
httpRequest = RestAssured.given().log().all();

File file1_new1 = new File(System.getProperty("user.dir")+"//src//main//resources//ImageUpload//1.jpg");
File file2_new2 = new File(System.getProperty("user.dir")+"//src//main//resources//ImageUpload//2.jpg");
File file3_new3 = new File(System.getProperty("user.dir")+"//src//main//resources//ImageUpload//3.jpg");

  httpRequest.given()
    .formParam("Year", "2008")
    .formParam("make", "Ford")
    .formParam("model", "Cascadia")
    .formParam("angleTokens", "top,left,bottom")
    .formParam("productCode", product_Code) 
    .multiPart("pictureData", file1_new, "image/jpeg")  
    .multiPart("pictureData", file2_new, "image/jpeg")
    .multiPart("pictureData", file3_new, "image/jpeg");

response = httpRequest.post("/UsedTrucksInventory/api/Image/UploadImage");
System.out.println("响应:"+response.asString()); }

关于java - 如何使用 RestAssured post 方法上传多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58998352/

相关文章:

java - Intellij Idea 无法执行 git pre-push hook

php - 使用外部 API 访问时,Python Bottle API 无 Json 响应

使用 AngularJS(Java 配置)上传 Spring Rest Multipart 文件?

java - 在 JAVA 中循环 SerenityRest 响应

java - 非 spring 依赖的 java 项目的 REST 确保测试

java - 尝试创建一个对象数组,数组的长度和数组中的对象均由用户确定

java - 如何确保 OverlayLayout 中的绘制顺序正确?

java - 用于命名要通过反射读取的类的注释

java - 在 WebLogic12.2.1 中禁用 WADL

java - 使用 cookie 和内容类型 Text/plain 的放心发布请求