【发布时间】:2015-04-14 09:07:44
【问题描述】:
我目前正在使用 Flask 在 Python 中设计一个 Web 服务。我现在很困惑它是一个 RESTful 服务还是一个普通的 web 服务。我一直在阅读很多关于 RESTful 服务的资料,但我仍然不能说我的服务是否是 REST 架构。 对我的 API 的请求是无状态的。
这是我所拥有的:
from flask import Flask,request
if __name__ == "__main__":
appLogger.info("RestFul service initialized and started")
app.run(host="0.0.0.0",port=int("80"),debug=True)
@app.route('/',methods = ['POST'])
def add():
"""
This function is mapped to the POST request of the REST interface
"""
#check if a JSON object is declared in the header
if request.headers['Content-Type'] == 'application/json; charset=UTF-8' and request.data:
try:
data = json.dumps(request.json)
#check if recieved JSON object is valid according to the scheme
#if (validateJSON(data)):
try:
saveToMongo(data)
appLogger.info("Record saved to MongoDB")
return "JSON Message saved in MongoDB"
except:
appLogger.error("Could not write to MongoDB")
except:
appLogger.error("Recieved invalid JSON")
else:
appLogger.error("Content-Type not defined or empty content")
raise FailedRequest
如果没有可能的响应,我将返回一个 json,它实际上是请求的有效负载。它始终是带有客户文本作为结果描述的常规 http 响应。 对吗,因为这个事实,它不是一个 RESTful 服务,如果我想称它为 RESTful 服务,我需要返回一个 json 对象吗?还是我完全错了?我的 API 只是一个简单的 RPC 吗?
【问题讨论】:
标签: python json web-services rest