question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OpenAPI v3 multipart/form-data

See original GitHub issue

Given the following OpenAPI v3 path:

  /multipart_file_upload:
    post:
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
      responses:
        200:
          description: File uploaded

It would be expected that the generated C# client would allow specifying a filename and data source for the file upload, with MultipartFormDataContent being used under the hood. However, the only parameter that is generated is System.IO.Stream body, which does not perform any sort of multipart encoding.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:14
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
algattikcommented, Apr 13, 2021

This workaround can be used in the client partial class:

    partial void PrepareRequest(HttpClient client, HttpRequestMessage request, string url)
    {
        if (request.Content is not StreamContent streamContent)
        {
            return;
        }

        if (streamContent.Headers.ContentType.MediaType != "multipart/form-data")
        {
            return;
        }

        var boundary = System.Guid.NewGuid().ToString();
        var content = new System.Net.Http.MultipartFormDataContent(boundary);
        content.Headers.Remove("Content-Type");
        content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
        content.Add(streamContent, "file", "file");
        request.Content = content;
    }
7reactions
CaptainBaxtercommented, Oct 29, 2019

I’m also having this issue with any IFormFile inputs on the controller. This worked fine with the Swagger 2.0 spec.

When generating a client for a multipart_file_upload using Swagger 2.0 spec a FileParameter(Stream data, string fileName, string contentType) would be generated and then within the client the following code would be generated for handling the boundaries:

var boundary_ = System.Guid.NewGuid().ToString();
var content_ = new System.Net.Http.MultipartFormDataContent(boundary_);
content_.Headers.Remove("Content-Type");
content_.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary_);
if (file != null)
{
    var content_file_ = new System.Net.Http.StreamContent(file.Data);
    if (!string.IsNullOrEmpty(file.ContentType))
         content_file_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(file.ContentType);
    content_.Add(content_file_, "file", file.FileName ?? "file");
}

Where file is of type FileParamter. This does not happen when generating code for the same endpoint using OpenApi3, instead, we do not generate a FileParameter class and pass in a Stream directly to the client api method. This means that the receiving server responds with a “missing content-type boundary”.

Let me know if I can provide any more information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multipart Requests
OAS 3 This guide is for OpenAPI 3.0. Multipart Requests. Multipart requests combine one or more sets of data into a single body,...
Read more >
How do I specify a multifile upload in OpenAPI?
I am trying to create an OpenAPI JSON input for the OpenAPI 3.0 UI for the multipart file upload. Here is what I...
Read more >
File upload on the V3 spec through multipart/form-data is ...
Run the function app. Access the OpenAPI document through http://localhost:7071/api/openapi/v3.json; Confirm whether it renders correctly.
Read more >
OpenAPI spec with multipart/form-data
We created an API from openapi spec. This is very simple spec with one path. But Select file field doesn't exists on the...
Read more >
Unable to edit the openAPI specification for multipart/form- ...
Hi, I have an API that use the multipart/form-data request, but I can't edit it in openAPI Specification. Is it not supported yet?...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found