我是制作API的初学者。我已关注博客https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask并能够创建Get-Post API方法。
我正在使用Flask制作Rest API。请参阅下面的代码,我想在API中将其作为输入,并通过将我的代码作为API返回JSON格式的答案。
app = Flask(__name__)
@app.route('/match/api/v1', methods = ['POST'])
def my_form_post():
if not request.json or not 'question' in request.json:
abort(400)
input_text_p = request.json['question'] # access input from curl request
reference_data = request.json['data'] # to access data field from the API request
path = 'airtel_faq.xlsx'
question_list, answer_list = read_excel_file(path) # reading some reference data from an excel file
input_text = input_text_p.translate(None, string.punctuation) # remove punctuation
final_answer = find_similarity(input_text, answer_list, question_list)
print "Final Answer is : ", final_answer
values = [
{'id' : 1,
'answer' : final_answer # answer I want in JSON
'done' : False
}
]
return jsonify({'values': values}), 201
if __name__ == '__main__':
app.run(debug = True)
我在本地计算机上运行脚本并将curl请求设置为:
curl -i -H "Content-Type: application/json" -X POST -d '{"input_question":"I want to check my internet plan", "data":"{"q":"haha","a":"wawa"}","type":"1"}"}' http://localhost:5000/match/api/v1
我的问题是我可以通过"数据":" {" q":"哈哈"," a&# 34;:" wawa"}" 作为使用curl的API的输入,因为数据中的q和a将是相关的问题集和asnwer。请让我知道如何做到这一点