简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM