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

[Python] Issue with Multipart/Form-Data Requests #12306

Open
jgpeiro opened this issue Dec 12, 2023 · 0 comments
Open

[Python] Issue with Multipart/Form-Data Requests #12306

jgpeiro opened this issue Dec 12, 2023 · 0 comments

Comments

@jgpeiro
Copy link

jgpeiro commented Dec 12, 2023

Description

I am encountering an issue with Swagger UI when using the Python generated code to perform multipart/form-data requests.
The server receives que request, but it fails validating the data. This happens both using the client code or the curl command.

http://localhost:8080/test validation error: '{\r\n  "name": "string",\r\n  "age": 0\r\n}' is not of type 'object' - 'body'
127.0.0.1 - - [12/Dec/2023 12:46:37] "POST /test HTTP/1.1" 400 -

Looking at the examples in the documentation, the root object is called body, and the file is binary so it is encoded octet-stream by default, so I don't see any errors here, but for some reason the server cannot validate it correctly .

Swagger-codegen version

https://editor-next.swagger.io/
OS: Ubuntu 22.04.2 LTS
Python: Python 3.10.12
Python packages:
swagger-ui-bundle 0.0.9
connexion 2.14.2
urllib3 2.1.0
Flask 2.2.5
six 1.16.0

Swagger declaration file content
openapi: 3.0.1
info:
  title: Test
  version: 1.0.0
servers:
  - url: http://localhost:8080
paths:
  /test:
    post:
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                body:
                  type: object
                  properties:
                    name:
                      type: string
                    age:
                      type: integer
                file:
                  type: string
                  format: binary
      responses:
        '200':
          description: OK
Steps to reproduce

It can be reproduced from python code or curl command:

from swagger_client.configuration import Configuration
from swagger_client.rest import ApiException
from swagger_client.api_client import ApiClient
from swagger_client.api.default_api import DefaultApi
from swagger_client.models import TestBody

configuration = Configuration()
configuration.host = "http://localhost:8080"

api_client = ApiClient(configuration)
api_instance = DefaultApi(api_client)

body = TestBody(
    name="test", 
    age=1
)
file="sample.bin"
try:
    api_response = api_instance.test_post(
        body=body.to_str(), # using .to_dict() method causes a TypeError exception on client side.
        file=file
    )
    print(api_response)
except ApiException as e:
    print("Exception when calling DefaultApi->test_post: %s\n" % e)
curl -X 'POST' \
  'http://localhost:8080/test' \
  -H 'accept: */*' \
  -H 'Content-Type: multipart/form-data' \
  -F 'body={
  "name": "string",
  "age": 0
}' \
  -F 'file=@sample.bin;type=application/octet-stream'
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

1 participant