二进制数据和文件

使用 bytes 类型在您的 API 中处理二进制数据

以 Markdown 格式查看

bytes 类型允许您在请求和响应中处理二进制数据。

发送字节

如果您的 API 需要发送字节流(即音频、图像和其他文件等资源的典型用法),那么您可以在 Fern 定义中使用 bytes 类型来建模。

audio.yml
1service:
2 base-path: /audio
3 endpoints:
4 upload:
5 display-name: Upload audio
6 method: POST
7 path: /upload
8 content-type: application/octet-stream
9 request:
10 type: bytes
11 docs: The bytes of the MP3 file that you would like to upload

接收字节

在响应中处理二进制数据时,使用 type: file 而不是 type: bytes

另一方面,如果您的 API 要返回字节流,那么您可以利用 bytes 类型作为响应。

textToSpeech.yml
1service:
2 base-path: /tts
3 endpoints:
4 upload:
5 display-name: Upload audio
6 method: POST
7 path: ""
8 request:
9 name: TTSRequest
10 body:
11 properties:
12 text:
13 type: string
14 docs: The text that you want converted to speech.
15 response:
16 type: file
17 docs: The bytes of the audio file.