Search code examples
restapihttphttpwebresponse

REST API - GET Request filter by Ids


Let's say I have an index endpoint to list units GET /projects/1/units/?id[]=1,2,3

and units with ids(1,2) belongs to project with id (1) but unit with id (3) doesn't belong to this project

What should be the proper response for this case, please note it's GET collection endpoint.

Response should be 200 with list of available units or 400 Bad Request because the unit with id=3 doesn't belong to the project?

Thanks


Solution

  • REST doesn't say anything about searching or the ability to retrieve multiple things in one API call. Remember that REST APIs should use hypermedia to indicate their state. This means that you can have an API that lists the units for a particular project as links.

    GET /projects/1/units
    
    {
      'self': {...}
      'links': [
        {
          'rel': 'unit',
          'href': '/projects/1/unit/1',
          'type': 'project.unit'
        },
        {
          'rel': 'unit',
          'href': '/projects/1/unit/2',
          'type': 'project.unit'
        }
      ]
    }
    

    Note that if unit 3 doesn't exist as part of project 1 then you should be returning a http status 404.