How to implement complex queries with a REST api?

前端 未结 2 1711
迷失自我
迷失自我 2021-02-19 07:13

I\'m building an EmberJS app using ember-data.

Some of the functionality in my app requires quite complex queries.

As an example, let\'s say I have three entitie

2条回答
  •  离开以前
    2021-02-19 07:38

    One option is to have a search endpoint on your students that you can post to.

    So you might have:

    POST /students/filter
    

    The filter object that you'd POST would look something like:

    { BornBefore:1993, TaughtBy:123 }
    

    I've also seen an option where instead of posting that, the API had a filter, and then used a query string.

    I prefer the first one myself. Especially if it can be a long running process, because your POST could return an ID and/or a rel link to the API call the client should use to get status updates and get the results.

    So then you'd POST /Students/filter and it would respond back with rel of /Students/Filter/123 and your client would do periodic GET's on /Students/Filter/123 until it got the result object. Of course, for simple, short queries, you could just instantly return the result. But if it was going to take more than a second or two you could go this route.

    This book by O'Reilly has some good information on structuring ReSTful APIs.

提交回复
热议问题