web2py rest api endpoint提供无效的路径输出

时间:2016-03-26 13:30:07

标签: api rest pattern-matching web2py

我制作了一个web2py网络应用程序。暴露的api端点如下。

"/comments[comments]"
 "/comments/id/{comments.id}"
 "/comments/id/{comments.id}/:field"
 "/comments/user-id/{comments.user_id}"
 "/comments/user-id/{comments.user_id}/:field"
 "/comments/date-commented/{comments.date_commented.year}"
 "/comments/date-commented/{comments.date_commented.year}/:field"
 "/comments/date-commented/{comments.date_commented.year}/{comments.date_commented.month}"
 "/comments/date-commented/{comments.date_commented.year}/{comments.date_commented.month}/:field"
 "/comments/date-commented/{comments.date_commented.year}/{comments.date_commented.month}/{comments.date_commented.day}"
 "/comments/date-commented/{comments.date_commented.year}/{comments.date_commented.month}/{comments.date_commented.day}/:field"
 "/comments/date-commented/{comments.date_commented.year}/{comments.date_commented.month}/{comments.date_commented.day}/{comments.date_commented.hour}"
 "/comments/date-commented/{comments.date_commented.year}/{comments.date_commented.month}/{comments.date_commented.day}/{comments.date_commented.hour}/:field"
 "/comments/date-commented/{comments.date_commented.year}/{comments.date_commented.month}/{comments.date_commented.day}/{comments.date_commented.hour}/{comments.date_commented.minute}"
 "/comments/date-commented/{comments.date_commented.year}/{comments.date_commented.month}/{comments.date_commented.day}/{comments.date_commented.hour}/{comments.date_commented.minute}/:field"
 "/comments/date-commented/{comments.date_commented.year}/{comments.date_commented.month}/{comments.date_commented.day}/{comments.date_commented.hour}/{comments.date_commented.minute}/{comments.date_commented.second}"
 "/comments/date-commented/{comments.date_commented.year}/{comments.date_commented.month}/{comments.date_commented.day}/{comments.date_commented.hour}/{comments.date_commented.minute}/{comments.date_commented.second}/:field"
 "/comments/complaint-id/{comments.complaint_id}"
 "/comments/complaint-id/{comments.complaint_id}/:field"

评论模型如下 模型/ db.py

db.define_table(
    'comments',
    Field('user_id', db.auth_user),
    Field('comment_made', 'string', length=2048),
    Field('date_commented', 'datetime', default=datetime.now),
    Field('complaint_id', db.complaints),
    Field('detailed_status', 'string', length=2048),
)

我已成功通过以下请求重新发送一条评论

localhost:8000/api/comments/id/1.json

现在我希望检索所有评论。我无法弄清楚如何使用/ comments [comments]来检索所有评论。 我试过了

localhost:8000/api/comments.json

但是它给出了一个“无效路径”的输出

我已经意识到http://localhost:8000/api/comments/complaint-id/1.json等请求  也给出“无效路径”作为输出。 请帮忙。

修改 控制器/ default.py

@request.restful()
def api():
    response.view='generic.' + request.extension
    def GET(*args,**kargs):
        patterns='auto'
        parser = db.parse_as_rest(patterns,args,kargs)
        if parser.status == 200:
            return dict(content=parser.response)
        else:
            raise HTTP(parser.status,parser.error)
    def POST(*args,**kargs):
        return dict()
    return locals()
主web2py文件夹中的

routes.py 更改默认应用程序:

routers = dict( 
    BASE = dict( 
        default_application='GRS', 
    ) 
)

另一个观察结果:

我添加了另一个端点,如下所示:

def comm():
    """" Comments api substitute"""
    rows=db().select(db.comments.ALL)       ## this line shows error
    # rows = db(db.comments.id > 0).select()
    #rows=[[5,6],[3,4],[1,2]]
    #for row in rows:
    #    print row.id
    return dict(message=rows)

即使是现在,我也无法使用“/comm.json”检索所有评论。这会在“rows = db.select(db.comments.ALL)”行上显示一条web2py错误标记,上面写着“需要超过1个值才能解压缩”。上述无效路径和此错误是否有关?

0 个答案:

没有答案