带有附加信息的 MediaType

MediaType with additional info

创建 Multipart/related 请求时。我们需要在 Content-Type 字符串

中添加一个额外的 type=MIME_TYPE

内容类型应如下所示

Content-Type: multipart/related; boundary=boundary_1324; type="application/json";

查看 MultipartBody 构造函数时

MultipartBody(ByteString boundary, MediaType type, List<Part> parts) {
    this.boundary = boundary;
    this.originalType = type;
    this.contentType = MediaType.parse(type + "; boundary=" + boundary.utf8());
    this.parts = Util.immutableList(parts);
  }

我们可以添加边界令牌。

但是当这样调用MediaType.parse

final MediaType parse = MediaType.parse("multipart/related; type=application/json");

对象解析为null

有人知道如何添加 type=application/json 吗?

引用值,不包括尾随 ;

MediaType mt = MediaType.parse("multipart/related; type=\"application/json\"");

这符合规范,它需要一个简单的标记或一个带引号的字符串。 https://www.rfc-editor.org/rfc/rfc2045#section-5.1

n.b。 MediaType 不会为您提取类型,但应该不会失败。