Java Multipart请求中的重复引号

发布于 2025-02-09 06:01:45 字数 3592 浏览 2 评论 0原文

我正在尝试使用重新安排提出多部分请求。该请求失败了,经过测试的API告诉我请求的“信息”部分不是对象。这是我的代码:

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);

现在这是奇怪的部分。标准重新安排的日志看起来像这样:

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>

有点拼命地确定问题的原因,我还打开了卷曲日志记录,这就是我所看到的:

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 "[email protected];type=image/jpg" ^
  --compressed ^
  --insecure ^
  --verbose

如您所见,这里的“信息”的内容具有重复的报价标记。这似乎是为什么失败的原因。我还将所有内容都复制到了邮递员中,并通过了预期的返回201。下面的日志和屏幕截图:

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": ""
  }

“在此处输入图像描述”

老实说,我不知道我在这里做错了什么。当我删除报价标记时,我没有引用标记。当我插入一个时,我会在卷曲日志中得到两个。我认为API并不是错误的,因为同样的请求使用Postman起作用。有人可以帮忙吗?

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¶m2=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¶m2=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 "[email protected];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¶m2=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": ""
  }

enter image description here

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪是无色的血 2025-02-16 06:01:45

在配置服务以显示到到达的内容的日志之后,我发现报价标记未重复。事实证明,卷曲记录仪为此负责。由于API中的错误,请求没有pas

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文