Skip to content

Generating a client for an endpoint that downloads binary data #3855

@kyleratti

Description

@kyleratti

I have an OpenApi endpoint that serves binary data (e.g., image/jpeg, image/png, image/gif, etc.) I am using Kiota to generate a C# API client. However, it seems that the generated client does not support endpoints that serve binary (OpenApi schema type file, format binary.)

As far as I can tell, this is the correct way to describe the endpoint. Is it possible to have Kiota generate a client function that can call this endpoint and return either a Stream (preferred) or a byte[]? I see vague mentions of defaulting to Stream for unknown mimetypes on #1471, however that is not the behavior I am seeing now.

I do see a warning about this when generating my client which makes me think my API description may be wrong?

The format binary is not supported by Kiota for the type binary and the string type will be used.

Here's the signature of the generated function:

public async Task<string?> GetAsync(Action<BytesRequestBuilderGetRequestConfiguration>? requestConfiguration = default, CancellationToken cancellationToken = default)

And here's my OpenApi Schema:

{
  "openapi": "3.0.1",
  "info": {
    "title": "WebApi",
    "version": "v1"
  },
  "paths": {
    "/image/{imageId}/bytes": {
      "get": {
        "tags": [
          "Image"
        ],
        "parameters": [
          {
            "name": "imageId",
            "in": "path",
            "required": true,
            "style": "simple",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "maxWidth",
            "in": "query",
            "style": "form",
            "schema": {
              "type": "integer",
              "format": "int32"
            }
          },
          {
            "name": "maxHeight",
            "in": "query",
            "style": "form",
            "schema": {
              "type": "integer",
              "format": "int32"
            }
          },
          {
            "name": "cropImage",
            "in": "query",
            "style": "form",
            "schema": {
              "type": "boolean"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "image/jpeg": {
                "schema": {
                  "type": "file",
                  "format": "binary"
                }
              },
              "image/png": {
                "schema": {
                  "type": "file",
                  "format": "binary"
                }
              },
              "image/gif": {
                "schema": {
                  "type": "file",
                  "format": "binary"
                }
              },
              "image/bmp": {
                "schema": {
                  "type": "file",
                  "format": "binary"
                }
              },
              "application/octet-stream": {
                "schema": {
                  "type": "file",
                  "format": "binary"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ImageNotFoundResult"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ImageNotFoundResult"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ImageNotFoundResult"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ImageNotFoundResult": {
        "type": "object",
        "properties": {
          "imageId": {
            "type": "string",
            "nullable": true
          },
          "message": {
            "type": "string",
            "nullable": true
          }
        },
        "additionalProperties": false
      }
    },
    "securitySchemes": {}
  },
  "security": []
}

This snag aside, I do want to say this is a fantastic tool and I'm very appreciative of all of the contributors!

Metadata

Metadata

Assignees

Labels

WIPgeneratorIssues or improvements relater to generation capabilities.help wantedIssue caused by core project dependency modules or librarytype:bugA broken experience

Type

No type

Projects

Status

Done ✔️

Relationships

None yet

Development

No branches or pull requests

Issue actions