RESTful API access sub-resource by type or id

657 Views Asked by At

I have some resources with 2 types of sub-resources. Each resource has its unique id. And I'd like to support accessing by id.

What I am doing currently is:

  • List sub-resources by type

    • GET /resources/type_1_subs
    • GET /resources/type_2_subs
  • List all sub-resources

    • GET /resources
  • Create sub-resource

    • POST /resources/type_1_subs
    • POST /resources/type_2_subs

I use separate POST, because input json schemas are different for 2 types.

To access resource by id, I think this is the natural way:

  • GET /resources/:id

But this looks strange, becuase the same part of uri has different meanings.

I think an alternative way is to access sub-resources with type as parameter.

  • List sub-resources by type

    • GET /resources?type=1
    • GET /resources?type=2
  • List all sub-resources

    • GET /resources
  • Get sub-resource by id

    • GET /resources/:id

For retrieving, this looks natural.

But for sub-resource creation, I am not sure what is the best practice.

  1. Use resource type as parameter, and input json unchanged.

    • POST /resources?type=1
    • POST /resources?type=2
  2. Put type to the input json

    • POST /resources
    • { "type": 1 or 2, TYPE_DEPENDENCY_FIELDS}

      I am not comfortable with this, because the json schema is not strict.

Any recommendation on the best practice on this scenario?

0

There are 0 best solutions below