Skip to content

Multipart

This doc details what emitters will generate for multipart request.

Common multipart request

When content-type of operation is “multipart/form-data”, the body payload is multipart payload.

model Address {
city: string;
}
model MultipartRequest {
id: string;
address: Address;
profileImage: bytes;
pictures: bytes[];
}
@post
op upload(
@header contentType: "multipart/form-data",
@body body: MultipartRequest,
): NoContentResponse;
class MultipartRequest:
id: str = rest_field()
address: "Address" = rest_field()
profile_image: FileType = rest_field(name="profileImage", is_multipart_file_input=True)
pictures: List[FileType] = rest_field(is_multipart_file_input=True)
def upload(
self, body: Union[_models.MultipartRequest, JSON], **kwargs: Any
) -> None:

Multipart request with @multipartBody

With @multipartBody, typespec author could define part of multipart request with more detailed info. For example, typespec author could use File model predefined in @typespec/http to declare the requiredness of filename and contentType.

model Address {
city: string;
}
model MultipartRequest {
id: HttpPart<string>;
address: HttpPart<Address>;
profileImage: HttpPart<File>;
previousAddresses: HttpPart<Address[]>;
pictures: HttpPart<File>[];
}
@post
op upload(
@header contentType: "multipart/form-data",
@multipartBody body: MultipartRequest,
): NoContentResponse;
class MultipartRequest:
id: str = rest_field()
address: "Address" = rest_field()
profile_image: FileType = rest_field(name="profileImage", is_multipart_file_input=True)
pictures: List[FileType] = rest_field(is_multipart_file_input=True)
def upload(
self, body: Union[_models.MultipartRequest, JSON], **kwargs: Any
) -> None: