简体   繁体   English

获取200状态代码而不是201或204

[英]Getting 200 status code instead of 201 or 204

I'm using Swagger 2.0 to create the framework of a python-flask backend. 我正在使用Swagger 2.0创建python烧瓶后端的框架。 I declared the POST /image endpoint as following on the Swagger online Editor: 我在Swagger在线编辑器上声明了POST /image端点,如下所示:

/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 . 下载python-flask服务器后,我使用命令python -m swagger_server运行它。 When sending a post request through postman I get that the status code is 200 instead of 201 . 通过邮递员发送发帖请求时,我得到的状态码是200而不是201 It also shows 200 on the command line where the server is running. 它还在运行服务器的命令行上显示200

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; 我很确定您需要在view函数中明确声明要返回的状态代码; the swagger description is just documentation that your API server doesn't actually care about. 粗略的描述只是您的API服务器实际上并不关心的文档。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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