有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Post多部分文件和Rest Assured中的JSON

我需要发送一个视频文件和JSON对象在休息放心后调用

结构如下所示:

{ "sample" : { "name" : "sample-name", "kind" : "upload", "video_file" : multipart file here } }

所以我喜欢下面的内容

代码:

given()
                        .header("Accept", "application/json")
                        .header(auth)
                        .config(rConfig)
                        .body(body)
                        .multiPart("sample[video_file]", new File("path"), "video/mp4")
                        .formParam("sample[name]", "Video Upload")
                        .formParam("sample[kind]", "upload")
                        .log().all().
                        expect()
                        .statusCode(expectedStatusCode)
                        .post(url);

在Rest-Assured中使用multipart时,我不能使用application/JSON。我以param的形式显式地硬编码了该值,并以多部分的形式发送了媒体文件,现在它工作正常

如何在单个内部对象中发送所有表单参数数据


共 (3) 个答案

  1. # 1 楼答案

    你的方法绝对不是标准的

    您不能有一个多部分请求和一个JSON主体,您需要从两种方法中选择一种:multipart/form-dataapplication/json请求

    标准方法是使用包含序列化json负载的“json”参数和包含多部分文件的“file”参数创建多部分请求

    given()
    .contentType(MediaType.MULTIPART_FORM_DATA_VALUE)
    .multiPart(file)
    .param("json", "{\"sample\":{\"name\":\"sample- name\",\"kind\":\"upload\",\"video_file\":<this is not needed>}}")
    

    但这涉及到更改服务器端逻辑

    如果无法更改服务器端逻辑,则需要将文件序列化为(例如作为字节数组或base64字符串),以便在JSON负载中设置为video_文件。在这种情况下,您将有一个application/json内容类型请求,而不是“多部分/表单数据”

  2. # 2 楼答案

    您可以使用RequestSpecBuilder来实现这一点。它支持所有请求参数,您可以轻松创建多部分请求

    示例代码取自https://github.com/rest-assured/rest-assured/wiki/Usage

    RequestSpecBuilder builder = new RequestSpecBuilder();
    builder.addParam("parameter1", "parameterValue");
    builder.addHeader("header1", "headerValue");
    RequestSpecification requestSpec = builder.build();
    
    given().
            spec(requestSpec).
            param("parameter2", "paramValue").
    when().
            get("/something").
    then().
            body("x.y.z", equalTo("something"));
    
  3. # 3 楼答案

    谢谢你的回复。我发布这个问题是为了用formParams处理内部对象。我已经为formParams创建了一个哈希映射。因为rest-assured的formParams方法可以接受哈希映射

    表单参数映射创建:

    private static Map<String, String> createFormParamsMap(VideoTagInput videoTag) {
    
            Map<String, String> formParams = new HashMap<>();
            formParams.put(createFormParamKey("name"), "name");
            formParams.put(createFormParamKey("kind"), "kind");
    
            return formParams;
    }
    
    private static String createFormParamKey(String paramKey) {
        return "sample[" + paramKey + "]"; 
        // output is like "sample[name]" - I'm forming inner object here for my purpose.
    }
    

    最后将地图发送到Rest Assured post call功能

    given()
                            .header("Accept", "application/json")
                            .header(auth)
                            .config(rConfig)
                            .multiPart("sample[video_file]", new File("path"), "video/mp4")
                            .formParams(requestParamsMap) // requestParamsMap here.
                            .log().all().
                            expect()
                            .statusCode(expectedStatusCode)
                            .post(url);