Connexion: How to upload binaries using connexion

Created on 1 Nov 2017  路  2Comments  路  Source: zalando/connexion

Description

How do I upload a file using connexion?
Here is a swagger snippet:

  /student:
    post:
      description: "Add a new student"
      operationId: "student_post"
      consumes:
      - "multipart/form-data"
      parameters:
      - in: "body"
        name: "body"
        description: "Student object"
        required: true
        schema:
          $ref: "#/definitions/Student"
      responses:
        200:
          description: "Student data."
        400:
          description: "Bad data"
      x-swagger-router-controller: "swagger_server.controllers.default_controller"
definitions:
  Student:
    type: "object"
    required:
    - "student_name"
    - "student_id"
    properties:
      student_name:
        type: "string"
      student_id:
        type: "number"
      date_added:
        type: "string"
        format: "date-time"
      pitcture1:
        type: "string"
      picture2:
        type: "string"

What should be the controller code?

I should be able to upload file using

$ curl -F "[email protected]" -F "[email protected]"  http://localhost:5000/student

Expected behaviour

This is more a question. I was not able to find any reference/pattern. The closest I got was https://github.com/zalando/connexion/issues/200

Actual behaviour

N/A

Steps to reproduce

N/A

Additional info:

Output of the commands:

  • $ python --version Python 3.6.3
  • $ pip show connexion | grep "^Version\:" Version: 1.1.9
question

Most helpful comment

For OpenAPI 3 I used this :

  /system/upgrade:
    post:
      summary: Upgrade bundle as downloaded from the NAbox web site
      operationId: api.system.post_upgrade
      tags:
      - system
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                fileName:
                  type: string
                  format: binary

Then in code :

def post_upgrade():
    uploaded_file = connexion.request.files['fileName']
    uploaded_file.save("/tmp/upgrade_bundle")
    return { 'status': 200 }

All 2 comments

Hi
I managed to do file uploads with form-data in the following way.
Of course this uploads the file as is and says nothing about the structure or contents of the file.
Your controller needs to know how to handle the contents.

/student
      post:
         description: "Add a new student"
         operationId: "student_post"
         consumes:
         - multipart/form-data
         parameters:
         - name: file_to_upload
            in: formData
            description: The file to be uploaded
            type: file
            required: true

Then in your controller you can access the file via:

def student_post():
      file_to_upload = connexion.request.files['file_to_upload']

For OpenAPI 3 I used this :

  /system/upgrade:
    post:
      summary: Upgrade bundle as downloaded from the NAbox web site
      operationId: api.system.post_upgrade
      tags:
      - system
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                fileName:
                  type: string
                  format: binary

Then in code :

def post_upgrade():
    uploaded_file = connexion.request.files['fileName']
    uploaded_file.save("/tmp/upgrade_bundle")
    return { 'status': 200 }
Was this page helpful?
0 / 5 - 0 ratings