tfiwsrets
tfiwsrets

Reputation: 87

Web API URL understanding

I have a question about web api. My models are User -* Budget -* Item. I'm sure that a method below is not right for getting an item

http://localhost/api/items/getitem/userid/budgetid/planFact/inOut/month/

and I have to authorize user first and then return only his data, but I don't know how can I do this.

and is it right to pass a lot of parameters in uri to retrieve needed data?

Upvotes: 0

Views: 40

Answers (1)

Eric Stein
Eric Stein

Reputation: 13692

Assuming this is a simple API, you should be able to get away with Basic Auth under SSL (https). Under this scheme, you'd pass the user's credentials up with every request in a header. That gives you ready access to the username for filtering out data. If this is homework, you can probably just say "and I'd use SSL in the real world" - ask your teacher.

As far as URI design, it's difficult to understand from your question what those path segments are supposed to represent, but you probably want to avoid anything that looks like a verb (such as getitem). Consider supporting these calls:

GET /items/{itemId}
// returns details on a specific item

GET /items?userId={userId}&budgetId={budgetId}
// returns a list of all items matching the query parameters
// may return just ids, limited detail, or as much detail as GET /items/{itemId}

It's not really clear what planFact and inOut are supposed to be. If month is to specify the items for a specific month, that should be a query parameter also.

It is likely that everyone will be happier if your URIs do not contain that many path segments.

Upvotes: 1

Related Questions