Skip to content

Commit

Permalink
multipart doc
Browse files Browse the repository at this point in the history
  • Loading branch information
msyyc committed Aug 27, 2024
1 parent 52a93d6 commit e7c8ca3
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions docs/howtos/Client Generation/12multipart.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# 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.

<Tabs>
<TabItem value="typespec" label="TypeSpec" default>

```typespec
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;
```

</TabItem>

<TabItem value="python" label="Python">

```python
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:
```

</TabItem>

<TabItem value="csharp" label="CSharp">

```csharp

```

</TabItem>

<TabItem value="typescript" label="Typescript">

```typescript

```

</TabItem>

<TabItem value="java" label="Java">

```java
```

</TabItem>
</Tabs>


## 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.

<Tabs>
<TabItem value="typespec" label="TypeSpec" default>

```typespec
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;
```

</TabItem>

<TabItem value="python" label="Python">

```python
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:
```

</TabItem>

<TabItem value="csharp" label="CSharp">

```csharp

```

</TabItem>

<TabItem value="typescript" label="Typescript">

```typescript

```

</TabItem>

<TabItem value="java" label="Java">

```java
```

</TabItem>
</Tabs>

0 comments on commit e7c8ca3

Please sign in to comment.