Bullish
Bullish

Reputation: 15

How to post a List<T> to a WCF Rest service

I have a class

[DataContract]
public class Test
{
[DataMemeber]
public string A {get;set;}
[DataMemeber]
public string B {get;set;}
[DataMemeber]
public string C {get;set;}
}

I have a Restful WCF method

[WebInvoke(UriTemplate = "checkupdates",
ResponseFormat = WebMessageFormat.Json, 
BodyStyle=WebMessageBodyStyle.WrappedRequest)]

List<Test> CheckForUpdates(List<Test> testing);

How can I post the List object to the service? This is from a wpf client.

Thanks

Upvotes: 0

Views: 2999

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87228

List is equivalent to an array, so the value should be represented as a JSON array. And since the body style says that the request needs to be wrapped, then you should wrap the JSON array in an object with a field named as the parameter:

{"testing":[
    {"A":"Value of A1","B":"Value of B1","C":"Value of C1"},
    {"A":"Value of A2","B":"Value of B2","C":"Value of C2"},
    {"A":"Value of A3","B":"Value of B3","C":"Value of C3"}]}

If the request were not wrapped (BodyStyle of Bare or WrappedResponse) then you wouldn't need the wrapping object, and this would be a request for the operation:

[
  {"A":"Value of A1","B":"Value of B1","C":"Value of C1"},
  {"A":"Value of A2","B":"Value of B2","C":"Value of C2"},
  {"A":"Value of A3","B":"Value of B3","C":"Value of C3"}
]

Upvotes: 1

Related Questions