Flask RESTful關於響應

2021-10-09 22:40:51 字數 3581 閱讀 8203

flask-restful 提供了marshal工具,用來幫助我們將資料序列化為特定格式的字典資料,以便作為檢視的返回值。

from flask_restful import resource, fields, marshal_with

resource_fields =

class

todo

(resource)

: @marshal_with(resource_fields, envelope=

'resource'

)def

get(self,

**kwargs)

:return db_get_todo(

)

也可以不使用裝飾器的方式

class

todo

(resource)

:def

get(self,

**kwargs)

: data = db_get_todo(

)return marshal(data, resource_fields)

示例
# 用來模擬要返回的資料物件的類

class

user

(object):

def__init__

(self, user_id, name, age)

: self.user_id = user_id

self.name = name

self.age = age

resoure_fields =

class

demo1resource

(resource)

: @marshal_with(resoure_fields, envelope=

'data1'

)def

get(self)

: user = user(1,

'itcast',12

)return user

class

demo2resource

(resource)

:def

get(self)

: user = user(1,

'itcast',12

)return marshal(user, resoure_fields, envelope=

'data2'

)

想要介面返回的json資料具有如下統一的格式

}

在介面處理正常的情況下, message返回ok即可,但是若想每個介面正確返回時省略message欄位

class

demoresource

(resource)

:def

get(self)

:return

對於諸如此類的介面,能否在某處統一格式化成上述需求格式?

}

flask-restful的api物件提供了乙個representation的裝飾器,允許定製返回資料的呈現格式

@api.representation(

)def

handle_json

(data, code, headers)

:# todo 此處新增自定義處理

return resp

flask-restful原始對於json的格式處理方式如下:

**出處:flask_restful.representations.json

from flask_restful.utils import py3

from json import dumps

defoutput_json

(data, code, headers=

none):

"""makes a flask response with a json encoded body"""

'restful_json',)

# if we're in debug mode, and the indent is not set, we set it to a

# reasonable value here. note that this won't override any existing value

# that was set. we also set the "sort_keys" value.

settings.setdefault(

'indent',4

) settings.setdefault(

'sort_keys'

,not py3)

# always end the json dumps with a new line

# see

dumped = dumps(data,

**settings)

+"\n"

resp = make_response(dumped, code)

resp.headers.extend(headers or

)return resp

為滿足需求,做如下改動即可

@api.representation(

)def

output_json

(data, code, headers=

none):

"""makes a flask response with a json encoded body"""

# 此處為自己新增***************

if'message'

notin data:

data =

# **************************

'restful_json',)

# if we're in debug mode, and the indent is not set, we set it to a

# reasonable value here. note that this won't override any existing value

# that was set. we also set the "sort_keys" value.

settings.setdefault(

'indent',4

) settings.setdefault(

'sort_keys'

,not py3)

# always end the json dumps with a new line

# see

dumped = dumps(data,

**settings)

+"\n"

resp = make_response(dumped, code)

resp.headers.extend(headers or

)return resp

Flask RESTful關於請求

flask restful 提供了requestparser類,用來幫助我們檢驗和轉換請求資料。from flask restful import reqparse parser reqparse.requestparser parser.add argument rate type int hel...

flask restful學習筆記

restful api前端與後台進行通訊的一套規範,使用這套規範可以讓前後端開發變得更加輕鬆 協議 http 或 https 資料傳輸格式 json url連線 get 獲取 post 新建 eg 使用者註冊 put 更新 eg 要求客戶端提供所有資料 patch 更新區域性 delete 從伺服器...

flask restful使用講解

flask restful筆記 安裝 flask restful需要在flask 0.8以上的版本,在python2.6或者python3.3上執行。通過pip install flask restful即可安裝。基本使用 1.從 flask restful 中匯入 api 來建立乙個 api 物件...