Roger
Roger

Reputation: 1631

ServiceStack Restful request using specific json

I need to create a request using Service stack that generates this JSON request:

[
    "ABC1234",
    "ABC5678",
    "ABC9122"
]

I tried this:

[Route("/getconsignments/{ConsignmentNumbers}", "POST")]
public class GetConsignments : IReturn<PublishManifestResponse>
{
    public string[] ConsignmentNumbers { get; set; }
}

public class PublishManifestResponse
{   
    [DataMember(Name = "ManifestNumber")]
    public string ManifestNumber { get; set; }
    [DataMember(Name = "ManifestedConnotes")]
    public string[] ManifestedConnotes { get; set; }
}

But it is not generating the request I need working.

Also,

How do I fill the json data in the request?

var request = new List<string>();
request.Add("abcd");
reuqest.Add("wewwq");

var client = new Client(new JsonServiceClient(appSettings.GetString("host")));
var result = client.GetConsignments(request.ToArray());

Something like that?

Upvotes: 1

Views: 38

Answers (1)

mythz
mythz

Reputation: 143399

You can accept a batched request by inheriting from a generic List, e.g:

[Route("/getconsignments/{ConsignmentNumber}", "POST")]
public class GetConsignments : List<string>, IReturn<PublishManifestResponse> 
{
    public string ConsignmentNumber { get; set; }
}

And if you need to return a JSON collection use IReturn<string[]>.

This will let you accept a Request conceptually similar to:

POST /getconsignments/1
[
    "ABC1234",
    "ABC5678",
    "ABC9122"
]

You shouldn't have complex types like a string[] in the /path/info portion of the route, if you also need to send ConsignmentNumbers[] in addition to the Request Body it should be specified on the QueryString instead which you'll need to remove from the Route, e.g:

[Route("/getconsignments", "POST")]
public class GetConsignments : List<string>, IReturn<PublishManifestResponse> 
{
    public string[] ConsignmentNumbers { get; set; }
}

Which will accept requests like:

POST /getconsignments?ConsignmentNumbers=1,2,3
[
    "ABC1234",
    "ABC5678",
    "ABC9122"
]

But if you just need the JSON array, you would just have an empty Request DTO definition, e.g:

[Route("/getconsignments", "POST")]
public class GetConsignments : List<string>, IReturn<PublishManifestResponse> {}

To accept requests like:

POST /getconsignments
[
    "ABC1234",
    "ABC5678",
    "ABC9122"
]

Upvotes: 1

Related Questions