Skip to content

Support non-file fields in Multipart requests #351

Closed

Description

@csymeonides-mf

Is your feature request related to a problem? Please describe.

Given this schema for a POST request:

{
  "requestBody": {
    "content": {
      "multipart/form-data": {
        "schema": {
          "type": "object",
          "properties": {
            "file": {
              "type": "string",
              "format": "binary"
            },
            "options": {
              "type": "string",
              "default": "{}"
            }
          }
        }
      }
    }
  }
}

the generated code will call httpx.post(files=dict(file=file, options=options)) which causes a server-side pydantic validation error (options should be of type str).

Describe the solution you'd like

The correct invocation would be httpx.post(data=dict(options=options), files=dict(file=file)) (see relevant page from httpx docs)

Describe alternatives you've considered

Some sort of server-side workaround, but it might be somewhat ugly.

Activity

  1. added
    ✨ enhancement
    on Mar 19, 2021
  2. csymeonides-mf commented on Mar 20, 2021

    @csymeonides-mf
    ContributorAuthor

    For reference, my server-side workaround was to add a custom pydantic validator for that field:

        @validator("options", pre=True)
        def convert_file_to_str(cls, options: Any) -> str:
            if isinstance(options, FileStorage):
                return options.stream.readline().decode()
            return options
    
  3. added a commit that references this issue on Mar 20, 2021
    61dee1e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✨ enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions