关注这篇文章: 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/