Stephen L.
Stephen L.

Reputation: 509

REST resources with parameters

Here is a problem. This is our simple domain: there is a number of questions and answers, a question may have several answers or doesn't have any.

Example:
question : "Where can I get a pen?".
answer #1:"You can buy a pen in a shop."
answer #2:"You can borrow it from a friend."

For the following situation it's possible to use the following REST resources:

GET /questions
GET /questions/{question_id}

GET /questions/{question_id}/answers
GET /questions/{question_id}/answers/{answer_id}

However some questions may have parameters like:

"What is the distance between {location A} and {location B}?"
"What is the status of flight {flight_number}?"

What is the best way to represent such questions and answers for them as REST resources?

Upvotes: 1

Views: 78

Answers (3)

inf3rno
inf3rno

Reputation: 26147

You can use the following links:

GET /questions/{question_id}/locationA:Zurich/locationB:Budapest/flightNumber:1234/answers

GET /questions/{question_id}/answers?locationA="Zurich"&locationB="Budapest"&flightNumber=1234

Now I am not sure you need the question id here. If there are limited number of question types, then you can add a path for each of them.

GET /questions/distance/from:"Zurich"/to:"Budapest"

You can generate this automatically from the question title:

GET /questions/what-is-the-distance/between:"Zurich"/and:"Budapest"

To be honest the URI structure does not really matter by REST services, because it is used only by machines and maybe by developers to configure the routing. REST clients should follow links instead of building URIs (HATEOAS constraint). So most of the APIs are not REST and most of their clients are not REST clients...

Upvotes: 1

Guanxi
Guanxi

Reputation: 3131

What about considering POST as here I understand you are getting the question, but at the same time you are creating a question with filling in the variables.

Provide the variables as a list of Key Value pairs (or Dictionary) in body.

Upvotes: 0

Mike Curry
Mike Curry

Reputation: 1609

Ok, I think you could build something like this

GET /question /{questionid}?location=a&location2=b

And

GET /question /{questionid}?number=12345

But, it is going to have a lot of things to consider.

Who defines what the parameter names would be? Is it the caller who asks the question? I guess without more of an idea of the actors involved in interacting with these services it is hard to be more specific.

Sorry, this is not as much help as I hoped it would be when I started. :)

Upvotes: 0

Related Questions