简体   繁体   中英

Restassured Parse Body response with boundary

I use RestAssured, and I'm trying to download files with de GET call.

My issue is that the request body is organised with a boundary.

So the file I get contain too many informations and is not usable. Here is an exemple of content of a simple text file downloaded :

--a8f82a89-d9c2-4fcb-a612-06e286bfcf66 Content-Disposition: form-data; name="metadata"; filename="metadata" Content-Type: application/json {"name":"Fichier_de_calcul.txt","id":{"metadata":{"deleted":false,"ephemeral":false,"synthesised":true,"systemOwned":true,"readOnly":false,"synchronisable":true,"createdOn":"2018-11-09T15:36:39Z","enabled":true,"lastModifiedOn":"2018-11-09T15:36:40.444Z"},"value":{"@type":"CoreKit.UUIDKey","value":"4de39a67-0075-4232-a236-09ab8d10aed9"}}} --a8f82a89-d9c2-4fcb-a612-06e286bfcf66 Content-Disposition: form-data; name="@type"; filename="@type" Content-Type: text/plain CoreKit.FileMetadataAndFile --a8f82a89-d9c2-4fcb-a612-06e286bfcf66 Content-Disposition: form-data; name="file"; filename="file" Content-Type: application/octet-stream 2 4 6 8 10 --a8f82a89-d9c2-4fcb-a612-06e286bfcf66--

And here is the code I used to get this file :

    Response resp = RestAssured.given().headers(headers).get(url);
    byte[] fileContent = resp.body().asByteArray();
    File fileToSave = new File(classPath);
    try {
        Files.write(fileContent, fileToSave);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

Does someone know how I can do to :

Parse the body (I'd like to get the "filename" parameter) Create the downloaded file based on the "application/octet-stream" part.

Thank you =)

Can you please try below appraoch: you can set all header which you have and get request with your url.

    Response response=RestAssured.given().header("Content-Type", "application/json").get("yourURL").then().contentType(ContentType.JSON).extract().response();
String finaleNAme=response.jsonPath().get("jsonValuewhichyouwanttoget");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM