【问题标题】:Convert Curl to Restassured将卷曲转换为放心
【发布时间】:2018-09-17 14:54:19
【问题描述】:

我有一个 cURL,它需要一个文件校验和和 xml 以及多个标题,以便将图像上传到服务器。 我对 RestAssured 不是很熟悉,所以我的问题是; 我如何在 RestAssured 中表示 -F cURL 值?

`curl -s -w "\nHTTP %{http_code}\n" -X POST ${mediaUrl}/api/user/${LCID}/repository/${REPO_NAME}/file?conflictSolve=copy \
-H "Accept: application/vnd.******.dv-1.19+xml" \
-H "Content-Type: multipart/form-data" \
-H "Authorization: *** token=\"${TOKEN}\"; authVersion=\"1.0\"" \
-H "x-******-***: ${TOKEN}" \
-H "X-Client-Identifier: TEST" \
-H "X-Client-Platform: CURL" \
-H "x-ingest-tag:RI" \
-F "files=@${XML};type=application/vnd.******.dv-1.19+xml" \
-F "${checksum}=@${file};type=${mimeType}"`

目前我只使用了标题。

        Response response = given()
            .log().all()

            .spec(RestUtilities.get**RequestSpecification())    
            .header("Accept","application/vnd.******.dv-1.19+xml")
            .header("Content-Type", "multipart/form-data")
            .header("Authorization", "*** token=\""+access_token+"\"; authVersion=\"1.0\";")
            .header("x-*****-***", "access_token")

    .when()
        .post()
    .then()
    .log().all()
        .statusCode(200)
        .extract()
        .response();

}

}

【问题讨论】:

    标签: rest curl rest-assured rest-assured-jsonpath


    【解决方案1】:

    属性 curl -F 将使用 Content-Type multipart/form-data http 标头生成带有数据的请求 POST。

    因此,使用 RestAssured 您必须调用方法 multipart (see the doc here) 来上传文件。

    这里是一个例子:

     @Test
    public void postMultipart() throws Exception {
        RestAssuredMockMvc.mockMvc(mockMvc);
        File xmlfile = resourceLoader.getResource("classpath:demo.xml").getFile();
        given()
                .log().all()
                .multiPart("files", xmlfile, "application/xml")
                .when()
                .post("/upload")
                .then()
                .log().all()
                .statusCode(200)
                .extract()
                .response();
    }
    

    所以参数 -F "files=@${XML};type=application/vnd.******.dv-1.19+xml" \

    可以转换为 .multiPart("文件", {YOUR-XML}, "application/vnd.******.dv-1.19+xml")

    您可以查看完整的源代码示例in my Github

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      • 2017-06-03
      • 2013-10-21
      相关资源
      最近更新 更多