web2py rest api endpoint gives invalid path output

2019-09-10 01:49发布

I have made a web2py web application. The api endpoints exposed are as follows.

"/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"

The comments model is as follows models/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),
)

I have been successful in retriving a single comment via the following request

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

Now I wish to retrieve all the comments. I am not able to figure out how to use /comments[comments] to retrieve all comments.? I have tried

localhost:8000/api/comments.json

But it gives an output with "invalid path"

I have realized requests such as http://localhost:8000/api/comments/complaint-id/1.json also give "invalid path" as output. Please help.

EDIT: Controllers/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()

routes.py in the main web2py folder to change the default application:

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

Another observation:

I added another endpoint as below:

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)

Even now I am not able to retrieve all comments with "/comm.json". This gives a web2py error ticket which says "need more than 1 value to unpack" on the line "rows=db.select(db.comments.ALL)". Are the above invalid path and this error related in someway?

0条回答
登录 后发表回答