Diego Aguado
Diego Aguado

Reputation: 1596

Getting 200 status code instead of 201 or 204

I'm using Swagger 2.0 to create the framework of a python-flask backend. I declared the POST /image endpoint as following on the Swagger online Editor:

/image:
    post:
      summary: "Description."
      operationId: "image_post"
      consumes:
      - "multipart/form-data"
      parameters:
      - in: "formData"
        name: "upfile"
        description: "The file to upload."
        required: true
        type: "file"
      responses:
        201:
          description: "Ok"
          schema:
            type: "string"
        400:
          description: "bad image"
      x-swagger-router-controller: "swagger_server.controllers.default_controller"

After downloading the python-flask server, I run it with the command python -m swagger_server. When sending a post request through postman I get that the status code is 200 instead of 201. It also shows 200 on the command line where the server is running.

Any clues if this is a bug? or am I doing something wrong?

Upvotes: 1

Views: 3165

Answers (1)

Brian
Brian

Reputation: 555

I'm pretty sure you need to explicitly declare which status code you want to return in your view function; the swagger description is just documentation that your API server doesn't actually care about. Something like this pseudo code:

def image():
    if request.method == 'POST':
        if not is_valid(request.form):
            return jsonify({'errors': [...]}), 400
        # do stuff
        return jsonify({'description': 'created'}), 201

Upvotes: 2

Related Questions