Sarah West
Sarah West

Reputation: 2037

Restful API with authentication

I try to built an API for my web2py application.

# -*- coding: utf-8 -*-
auth.settings.allow_basic_login = True

@auth.requires_login()
@request.restful()
def api():
    response.view = 'generic.json'

    def GET(tablename, id):
        if not tablename == 'division':
            raise HTTP(400)


        result = db(db.division.title == id).select()

        return dict(result = result)

return locals()

But everytime I try to connect via curl I get this answer:

You are being redirected <a href=\"/my_manager/login?_next=/my_manager/api/get_all_divisions/1.json\">here</a>

When I comment out this line

@auth.requires_login()

everythink works fine.

I've searched for hints, but didn't found any helpful information about this topic so far.

Any help is really appreciated.

Upvotes: 1

Views: 1281

Answers (1)

Muhammad Bilal
Muhammad Bilal

Reputation: 2134

I know its a pretty old question but still this answer will help other people who might experience same issue:

Well in your case you are using @auth.requires_login() which needs user to be logged in.

So you need to send basic auth credentials while making calls to this api.

A simplest curl call can be:

curl --user [email protected]:abc123 http://localhost:8000/app_name/default/api/table_name.json

Here:

  • [email protected] is username
  • abc123 is password
  • app_name is the web2py's app name
  • table_name is the table name in db

Upvotes: 1

Related Questions