Search code examples
restarchitecturerestful-urlrestful-architecture

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?


Solution

  • 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...