我尝试添加模式验证,就像在官方的cornice文档中描述的那样,并尝试通过service_name.post(schema=SomeSchemaClass)等装饰器来添加模式验证,但它不起作用
import colander
class TrackSchema(colander.MappingSchema):
genre = colander.SchemaNode(colander.String(), location="body", type='str')
@track.post(schema=TrackSchema)
def create_track(request):
...我得到了错误
"status": "error", "errors": [{"location": "body", "name": null, "description": "Expecting value: line 1 column 2 (char 1)"}, {"location": "body", "name": "genre", "description": "genre is missing"}]}我尝试了不同类型的location arg,比如querystring和headers,但是我得到了相同的错误。
发布于 2013-07-06 15:10:58
在深入研究之后,如果你查看cornice.util.extract_request_data函数,你会发现它试图以json json.loads(request.body)的形式加载主体,所以你需要将你的数据作为json来发布:
curl -H "Content-Type: application/json" -X POST http://localhost:6543/foo -d '{"foo": "bar"}'HTH
https://stackoverflow.com/questions/16058400
复制相似问题