简体   繁体   中英

Customize parameter splitting in ServiceStack Route

I have a REST endpoint that allows clients to get values for one or multiple variables. I'm using ServiceStack to achieve this. The issue arises from how ServiceStack parses multiple variables. It seems that the default behavior is to split the "Variables" parameter string (see Request DTO VariableRequest below) it just splits the string on commas. The problem with this is that some of my variables are multi-dimensional arrays, and we allow users to specify an index of that array, but to do that the user has to separate the indices with commas. This means that ServiceStack is splitting some of my variables in half.

For example here are two possible urls for this endpoint:

  1. localhost:8080/get/t_f1,t_f2
  2. localhost:8080/get/t_f1,array(1,2)

The first one splits appropriately to:

{"t_f1","t_f2"}

The second one splits to:

{"t_f1","array(1","2)"}

But I want it to split to:

{"t_f1","array(1,2)"}

I'm trying to avoid two possible solutions (though they may be unavoidable):

  1. Process the Variables string array (generated by ServiceStack) and fix incorrect splits. (this feels too hacky to me)
  2. Change the url endpoint. For example dressing up the parameters with quotations. (Power users will be directly querying this service by entering urls directly into the browser, so I like the simplicity of the current endpoint structure)

My ideal solution would be to somehow customize how ServiceStack splits these parameters directly. I could not find a solution in ServiceStack's documentation though (to be fair, I could've missed something or misunderstood how to apply something like urlfilters).

Any ideas?

[Route("/get/{Variables}"]
public class VariableRequest{
    public string[] Variables {get; set;}
}

If you need custom route parsing you'll need to do it yourself using a Wildcard path , eg:

[Route("/get/{Variables*}"]
public class VariableRequest
{
    public string Variables {get; set;}
}

Then in your Request you can parse the raw request.Variables string yourself.

Although I would avoid including any complex types in the /path/info and pass them via the QueryString instead. So in this case I would remove Variables from the Route /path/info:

[Route("/get"]
public class VariableRequest
{
    public string Variables {get; set;}
}

Which will then let you call your Services with /get?Variables=.... .

IMO /get isn't a great choice for a REST-ful endpoint, I'd personally define this Route like:

[Route("/vars"]
public class GetVariables : IReturn<GetVariablesResponse>
{
    public string Symbols { get; set; }
}

So the Service can be called with /vars?symbols=... . For more info on designing APIs please see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM