Skip to content

Commit 9950277

Browse files
committed
chore: [JS] Document Multipart Request Feature
1 parent e5fab3d commit 9950277

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

docs-js/features/openapi/execute-openapi-request.mdx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,51 @@ const httpResponse: HttpResponse = MyEntity.requestBuilder()
152152

153153
<ExecuteRawUseCases />
154154

155+
### Sending Multipart Requests
156+
157+
The OpenAPI generator supports APIs that use multipart (`multipart/form-data`) for file uploads and mixed content types.
158+
The SAP Cloud SDK automatically encodes your request body as multipart form-data based on the content types specified in your OpenAPI document:
159+
160+
```ts
161+
const response = await MyApi.uploadDocument({
162+
title: 'My Document', // text/plain
163+
file: new Blob([]), // application/octet-stream
164+
metadata: { pages: 10 } // application/json
165+
}).execute(destination);
166+
```
167+
168+
#### File Uploads with Blob or File
169+
170+
For binary content like files or images, provide `Blob` or `File` instances.
171+
Use `File` instead of `Blob` when you need to specify a filename, which may be required by the API.
172+
173+
```ts
174+
import { openAsBlob } from 'node:fs';
175+
176+
const pdf = await openAsBlob('path/to/file.pdf', { type: 'application/pdf' });
177+
const response = await MyApi.uploadDocument({
178+
file: pdf,
179+
description: 'My PDF file'
180+
}).execute(destination);
181+
```
182+
183+
Using `Blob` or `File` instances bypasses automatic property encoding for that field and preserves the binary data.
184+
The SAP Cloud SDK makes a best-effort attempt to warn about content type-mismatches and apply character encoding (e.g. `utf-8`, `latin1`) based on your OpenAPI specification.
185+
186+
#### Full Control with FormData
187+
188+
For complete control over the multipart request, provide a `FormData` instance directly.
189+
190+
```ts
191+
const formData = new FormData();
192+
formData.append('file', blob, 'document.pdf', { type: 'application/pdf' });
193+
formData.append('metadata', JSON.stringify({ title: 'My Document' }));
194+
195+
const response = await MyApi.uploadDocument(formData).execute(destination);
196+
```
197+
198+
When using raw `FormData`, you are responsible for properly formatting all parts according to your API's requirements.
199+
155200
## Handling of Cross-Site Request Forgery Tokens
156201

157202
<Csrf />

0 commit comments

Comments
 (0)