以下是这篇文章: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页面上。我不知道这个问题是什么.
谢谢你的帮助。
发布于 2018-10-26 23:50:46
您的缩进似乎是错误的,您可以在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发布于 2018-10-26 15:05:05
放入操作id: add_entry或去掉标签操作id
https://stackoverflow.com/questions/43711715
复制相似问题