Skip to content

Support non-file fields in Multipart requests #351

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

Closed
csymeonides-mf opened this issue Mar 19, 2021 · 1 comment
Closed

Support non-file fields in Multipart requests #351

csymeonides-mf opened this issue Mar 19, 2021 · 1 comment
Labels
✨ enhancement New feature or improvement

Comments

@csymeonides-mf
Copy link
Contributor

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.

@csymeonides-mf
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant