【问题标题】:Can Content-Disposition filename be used for text fields in multipart forms with Spring Webclient?可以将 Content-Disposition 文件名用于 Spring Webclient 的多部分表单中的文本字段吗?
【发布时间】:2019-10-17 12:59:43
【问题描述】:

使用 Service-Now API(例如)需要请求提供以下内容:

POST /api/now/attachment/upload HTTP/1.1
Host: somehost.testenv.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MadHyZrFTrZu0gW
cache-control: no-cache
Content-Disposition: form-data; name="table_name"

some_table

Content-Disposition: form-data; name="table_sys_id"

82c9aca7kljasdfkljhasdfec8dfdb61d961920

Content-Disposition: form-data; name="uploadFile"; filename="undefined"

Content-Type: file

------WebKitFormBoundary7MA4YWxkTrZu0gW—

以上是使用 Postman 生成的,并提供 form-data 键值对。

但是,根据定义 Content-Disposition 的 RFC 的摘要:https://www.rfc-editor.org/rfc/rfc2183

Two values for this
header field are described in this memo; one for the ordinary linear
presentation of the body part, and another to facilitate the use of
mail to transfer files.

Spring Webflux (https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ContentDisposition.html) 中使用的 Content-Disposition 类仅包含“文件名”的内部字段,它是一个字符串。

org.springframework.http 中的 ContentDisposition 类是否缺少 RFC-2183 中关于标准值(正文部分的普通线性表示)的组件?

spring代码自动生成的HTTP请求如下:

POST /api/now/attachment/upload HTTP/1.1
user-agent: ReactorNetty/0.7.9.RELEASE
transfer-encoding: chunked
host: somehost.testenv.com
accept: */*
accept-encoding: gzip
Content-Type: multipart/form-data;boundary=o-0JsSvUdGo98NDHJSWTwjvgzlRSXsmJ98-pWQ;charset=UTF-8

这是生成上述HTTP请求的源代码:

MultipartBodyBuilder mbuilder = new MultipartBodyBuilder();
mbuilder.part("table_name", snConfig.getChangeRecordTableName());
mbuilder.part("table_sys_id", result.get(0).getSysId());
mbuilder.part("uploadFile", someFile);

return client.post()
    .uri(snConfig.getEndpointAttachmentUpload())
    .contentType(MediaType.MULTIPART_FORM_DATA)
    .syncBody(mbuilder.build())
    .retrieve()
    .bodyToMono(AttachmentUploadResult.class);

看起来代码将所有 MultipartBodyBuilder 组件存储到单个 multipart/form-data 中,这与工作 HTTP 请求的格式不同。

但是,即使通过手动提供 Content-Disposition 标头,也只能创建一个名称/值对,其中值为“文件名”。

Content-Disposition 是否缺少功能? WebClient 可以做到这一点吗?

【问题讨论】:

    标签: spring spring-boot http-headers spring-webflux content-disposition


    【解决方案1】:

    在研究了很长时间之后,WebClient 似乎不支持此功能。

    【讨论】:

      猜你喜欢
      • 2010-12-02
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2016-11-06
      • 2019-12-29
      相关资源
      最近更新 更多