Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass list of complex types in query string?

Tags:

servicestack

How would I pass a list of complex types in ServiceStack? For example my Request DTO looks like this:

//Request DTO
public class Test
{
    public IList<Fund> Funds { get; set; }
}

public class Fund
{
    public string Key { get; set; }
    public int Percent { get; set; }
}

How can I pass the serialized object via HTTP get?

http://localhost:49490/api/funds={ ?? }

KeyValueDataContractDeserializer: Error converting to type: Type definitions should start with a '{', expecting serialized type 'Fund', got string starting with: asdf

like image 381
Renato Heeb Avatar asked Feb 24 '12 11:02

Renato Heeb


People also ask

What is query string with example?

A query string commonly includes fields added to a base URL by a Web browser or other client application, for example as part of an HTML, choosing the appearance of a page, or jumping to positions in multimedia content.

What is in a query string?

A querystring is a set of characters input to a computer or Web browser and sent to a query program to recover specific information from a database .


1 Answers

ServiceStack parses the queryString using the JSV Format this is basically JSON with the CSV-style quotes (i.e. only needs quotes when your value has an escape char).

Although you haven't defined a Custom Route here, in most cases your custom route is the same as your Request DTO which in this case is Test not /funds.

So assuming a custom route looks like:

Routes.Add<Test>("/test");

You can call your service via a QueryString like:

http://localhost:49490/api/test?Funds=[{Key:Key1,Percent:1},{Key:Key2,Percent:2}]

On a side note Interfaces on DTOs are generally a bad idea, you should consider avoiding (at least limiting) its use at all times.

like image 124
mythz Avatar answered Sep 27 '22 19:09

mythz