Some optional REST API GET request string parameters

I use web api 2 to implement the service. Having done some research on best practices, everyone seems to have different opinions on how to do the following. I have got

public HttpResponseMessage Get(string crewId, string shiftDate, int offset = 1, int limit = 10)

This GET method returns a list. There are several ways to get data from this method.

Get crew only Get only with shiftDate or Get with crewId and shiftDate

You (1) Mark crewId and shiftDate as optional?

public HttpResponseMessage Get(string crewId = null, string shiftDate = null, int offset = 1, int limit = 10)

and then there are a bunch of if statements to check what is filled and what is not filled in order to be able to do actions.

if(crewId != null && shiftDate == null){
  // Get by crewId
}else if(crewId == null && shiftDate != null){
  // Get By shiftDate
}else if(crewId != null && shiftDate != null){
  // Get By crewId and shiftDate
}

This seems crazy to me, especially if you have a lot of options, you will have too many ifs in your code.

Do you (2) have different settings?

public HttpResponseMessage GetByCrewId(string crewId, int offset = 1, int limit = 10)
public HttpResponseMessage GetByShiftDate(string shiftDate, int offset = 1, int limit = 10)
public HttpResponseMessage GetByCrewIdShiftDate(string crewId, string shiftDate, int offset = 1, int limit = 10)

and then you will have a route map to the URI method

... / api / GetByCrewId? CrewId = 1234 ... / api / GetByShiftDate? ShiftDate = 1111-11-11 ... / api / GetByCrewIdShiftDate crewId = 1234 &? ShiftDate = 1111-11-11

is option 2 calm?

Or there are better options (3).

Both of the options above will only work to ensure that I am using best practices and following REST standards. It just seems like I'm missing something, and hopefully you can put me in the right direction.

+4
source share
4 answers

You don't want parameter (2) to be RESTful, you want nouns in your URL.

So, you want your URL to look like this:

/api/items/?crewId=1234&shiftDate=1111-11-11

( , , "Crew" "Shift". ? ? , URL as/api/fishing-trip/? crewId =.... & shiftDate =...

, - :

public HttpResponseMessage Get(string crewId = null, string shiftDate = null, int offset = 1, int limit = 10) {

    return dataSource.Where(x=> (x.CrewId == crewId || crewId == null)
            && (x.ShiftDate == shiftDate || shiftDate == null));
}
+5

- . , :

public HttpResponseMessage Get(string crewId = null, string shiftDate = null, int offset = 1, int limit = 10)

. , - :

var query = "";
if (!String.IsNullOrEmpty(crewId)) {
  query += $"crewId='{crewId}'";
}
if (!String.IsNullOrEmpty(shiftDate)) {
  if (query.Length > 0) query += " AND ";
  query += $"shiftDate='{shiftDate}'";
}
if (query.Length == 0) {
  //neither crewId or shiftDate were given
  //return some kind of error
}
+2

: REST , , . , - , URI.

/api/fishing-trips/crew/{crewid}?shiftdate=1111-11-11

, , "?" , . RFC 6570.

, , , . REST.

, , , , , REST.

,

/api/fishing-trips/search?crew=1234
/api/fishing-trips/search?shiftDate=1111-11-11
/api/fishing-trips/search?crew=1234&shiftDate=1111-11-11

,

/api/fishing-trips/today
/api/fishing-trips/today?crew=1234
/api/fishing-trips/crew/1234/today

, " Rest API" URL- RESTful .

+2

, , , GET .

? REST API. , AND (.. CrewId = 1 AND ShiftDate = 2016-01-01). , .

I will pass my parameters to the end of the SQL stored procedure, which specified default values ​​and will return results based on the parameters passed.

Remember that in many ways, REST methods map directly to CRUD, so treat your API as such: REST API Tutorial

+1
source

Source: https://habr.com/ru/post/1624995/


All Articles