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.
- TypeSpec
- Python
- CSharp
- Typescript
- Java
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:
// Model class
@Immutable
public final class MultipartRequest {
public MultipartRequest(String id, Address address, ProfileImageFileDetails profileImage, List<PicturesFileDetails> pictures);
}
// Client API
public void upload(MultipartRequest body);
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.
- TypeSpec
- Python
- CSharp
- Typescript
- Java
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:
// Model class
@Immutable
public final class MultipartRequest {
public MultipartRequest(String id, Address address, FileDetails profileImage, List<Address> previousAddresses, List<FileDetails> pictures);
}
// Client API
public void upload(MultipartRequest body);