python - 用于 POST JSON API 的 flasgger YAML

标签 python api post flask swagger

关注这篇文章: Get Data JSON in Flask

我能够编写一个简单的 API,我在其中发布一个 json 对象(名字和姓氏以及生成的 ID)以将其插入到数据库中。

之后想用swagger/flasgger来建模。 它已经为 get 或 get_by_id 工作,但不可能为 POST 工作。

在我的 python 代码中我有这个:

from flask import Flask, jsonify, request
@app.route('/api/v1/list', methods=['POST'])
@swag_from('index_post.yml')
def add_entry():
    request_json = request.get_json()
    value1 = request_json.get('First_Name')
    value2 = request_json.get('Last_Name')
    if value1 is not None and value2 is not None:
        cursor.execute("INSERT INTO person (first_name,last_name) VALUES 
        (%s,%s)", (value1, value2))
    data = conn.commit()
    return jsonify(data)

在 YAML 文件中,我有:

paths:
/api/v1/list:
post:
description: testpost
operationId: PostNewName
consumes:
  - application/json
parameters:
  - name: body
    in: body
    required: true
    schema:
      id : toto
      required:
        - first
        - last
      properties:
        first:
          type: string
          description: Unique identifier representing a First Name
        last:
          type: string
          description: Unique identifier representing a Last Name
responses:
  200:
    description: creation OK

但是参数没有出现在 swagger html 页面上。 我不知道这个问题是什么......

感谢您的帮助。

最佳答案

您的缩进似乎有误,您可以在 https://editor.swagger.io/ 编辑和验证您的架构.我开始修复它,但我认为您现在应该可以完成剩下的工作:

swagger: '2.0'
info:
  title: Demo App
  version: "1"
paths:
  /api/v1/list:
    post:
      description: testpost
      operationId: PostNewName
      consumes:
        - application/json
      parameters:
        - name: body
          in: body
          required: true
          schema:
            id : toto
            required:
              - first
              - last
            properties:
              first:
                type: string
                description: Unique identifier representing a First Name
              last:
                type: string
                description: Unique identifier representing a Last Name
      responses:
        200:
          description: creation OK

关于python - 用于 POST JSON API 的 flasgger YAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43711715/

相关文章:

python - 故意错误的代码(Python)

Python if 和 else 打印条件

Python:sqlite 没有找到与 sqlite 匹配的分布

php - 如何在 Laravel 中使用 JSON 对象中的 POST 方法获取数据?

python - PageRank 计算结果不正确

Facebook 图 SINCE 和 UNTIL 不起作用

javascript - 无法使用 Asp.Net 查看 Google map ,错误为 :InvalidKeyOrUnauthorizedURLmapError

android - Volley POST 请求收到错误结果

post - HTTP 发布请求 : 401 (Unauthorized)

r - 使用 r 导航和抓取带有下拉 html 表单的网页