首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于发布JSON API的flasgger YAML

用于发布JSON API的flasgger YAML
EN

Stack Overflow用户
提问于 2017-05-01 05:51:13
回答 2查看 4.6K关注 0票数 3

以下是这篇文章:Get Data JSON in Flask

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

之后,我想使用swagger/flasgger对其进行建模。它已经为get或get_by_id工作了,但是POST不可能工作。

在我的python代码中,我有这样的代码:

代码语言:javascript
复制
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文件中,我有:

代码语言:javascript
复制
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页面上。我不知道这个问题是什么.

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

发布于 2018-10-26 23:50:46

您的缩进似乎是错误的,您可以在https://editor.swagger.io/上编辑和验证您的模式。我开始修复它,但我认为您现在应该能够完成剩下的工作:

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2018-10-26 15:05:05

放入操作id: add_entry或去掉标签操作id

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43711715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档