简体   繁体   中英

duplicated quotation marks in java multipart request

I'm trying to make a multipart request using restassured. The request fails with the tested API telling me that the "information" part of the request is not an object. Here is my code:

RequestSpecification request;


        Options options = Options.builder()
                .printMultiliner()
                .build();
        RestAssuredConfig config = CurlLoggingRestAssuredConfigFactory.createConfig(options).multiPartConfig(MultiPartConfig.multiPartConfig().defaultCharset(StandardCharsets.UTF_8));

request = given()
                .relaxedHTTPSValidation()
                .config(config)
                .header("Authorization", "Bearer " + LoginSteps.accessToken)
                .queryParam("memberId", memberId)
                .queryParam("policyId", policyId)
                .contentType(String.valueOf(ContentType.MULTIPART_FORM_DATA))
                .multiPart("information", "{\"description\":\"info\"}")
                .multiPart("documents", new File(filePath), contentType)
        .log().everything();

        return request.post(baseUrl + endpoint);

and now here is the weird part. The standard restassured log looks like this:

Request method: POST
Request URI:    myUri/?param1=149479812&param2=19281999
Proxy:          <none>
Request params: <none>
Query params:   param1=149479812
                param2=19281999
Form params:    <none>
Path params:    <none>
Headers:        Authorization=Bearer aToken
                Accept=*/*
                Content-Type=multipart/form-data; charset=ISO-8859-1
Cookies:        <none>
Multiparts:     ------------
                Content-Disposition: form-data; charset=ISO-8859-1; name = information; filename = file
                Content-Type: text/plain

                {"description":"info"}
                ------------
                Content-Disposition: form-data; charset=ISO-8859-1; name = documents; filename = testFile.jpg
                Content-Type: image/jpg

                src\test\resources\testData\testFile.jpg
Body:           <none>

Being a bit desperate to determine the cause of the issue, I also turned on curl logging and here's what I saw:

13:58:05.312 [main] DEBUG curl - curl "myUri/?param1=149479812&param2=19281999" ^
  --request POST ^
  --header "Authorization: Bearer aToken ^
  --header "Accept: */*" ^
  --header "Host: theHost" ^
  --header "Connection: Keep-Alive" ^
  --header "User-Agent: Apache-HttpClient/4.5.13 (Java/11)" ^
  --form "information={""description"":""info""};type=text/plain; charset=US-ASCII" ^
  --form "documents=@testFile.jpg;type=image/jpg" ^
  --compressed ^
  --insecure ^
  --verbose

As you can see, the content of "information" here has duplicated quotation marks. Which seems to be the cause of why this is failing. I also copy-pasted everything into Postman and it passes, and returns a 201 as expected. Log and screenshot below:

POST myUri/?param1=149479812&param2=19281999 {
  
  "Request Headers": {
    "authorization": "Bearer aToken"
    "user-agent": "PostmanRuntime/7.29.0",
    "accept": "*/*",
    "postman-token": "bb25e9db-87c0-4fdd-959a-e4229ed36e7a",
    "host": "host",
    "accept-encoding": "gzip, deflate, br",
    "connection": "keep-alive",
    "content-type": "multipart/form-data; boundary=--------------------------115811667537577795637808",
    "content-length": "28196"
  },
  "Request Body": {
    "information": "{\"description\":\"info\"}",
    "documents": ""
  }

在此处输入图像描述

I have honestly no idea what I'm doing wrong here. When I remove the quotation marks, I get no quotation marks. when I insert one I get two in the curl log. I don't think the API is at fault, as the same request works using postman. Can anyone help, please?

After configuring the service to show logs of what requests are arriving, I found out that the quotation marks are not duplicated. It turns out the curl logger was responsible for that. And the request did not pas due to a bug in the API

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