跳到导航

二进制数据和文件

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

以 Markdown 格式查看

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

发送字节

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

audio.yml
service:
base-path: /audio
endpoints:
upload:
display-name: Upload audio
method: POST
path: /upload
content-type: application/octet-stream
request:
type: bytes
docs: The bytes of the MP3 file that you would like to upload

接收字节

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

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

textToSpeech.yml
service:
base-path: /tts
endpoints:
upload:
display-name: Upload audio
method: POST
path: ""
request:
name: TTSRequest
body:
properties:
text:
type: string
docs: The text that you want converted to speech.
response:
type: file
docs: The bytes of the audio file.