Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return binary in API #959

Closed
wangjia184 opened this issue Jun 13, 2024 · 1 comment
Closed

Return binary in API #959

wangjia184 opened this issue Jun 13, 2024 · 1 comment

Comments

@wangjia184
Copy link

Hi

I want to implement an API to return an image as raw binary. Content-type should be image/png .
So when this API is opened in web browser, the image is presented.

After reading the example https://github.com/juhaku/utoipa/blob/d020f92a0446adc7e99999aa9e1be63e95728fbb/examples/rocket-todo/src/main.rs, I still don't find a way.

The IntoResponses and ToResponse traits seems does not allow to return raw binary body?

@juhaku
Copy link
Owner

juhaku commented Oct 1, 2024

It should be possible, though there are no examples for it unfortunately. E.g. for OpenApi 3.0 you can do something like this.

#[derive(ToSchema)]
#[schema(format = Binary, value_type = String)]
struct MyFile(Vec<u8>);

#[utoipa::path(
   get,
   path = "/my-file",
   responses(
       (status = 200, description = "My file", content_type = "image/png", body = MyFile),
   )
)]
fn handler() {}

This will change a bit for OpenApi 3.1 since there are specific attributes that can be set like contentEncoding and contentMediaType.

Relevant link for what is going to change in utoipa 5.0.0 https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants