1

I am trying to POST JSON to a WCF service. The json object contains a property and an array. The problem is the TripleTransaction is always null when the request arrives at the server. I'm wondering how to correctly bind to my data contract. If anyone can give me a pointer here I would really appreciate it.

This is what my service interface looks like:

 [OperationContract]
        [CorsBehavior]
        [WebInvoke(Method = "POST", UriTemplate = "Triples/{library}", ResponseFormat = WebMessageFormat.Json)]
        ResultMessage InvokeGraphTransactions(string library, TripleTransaction tripleTransaction);

I am attempting to populate the TripleTransaction with the POSTed JSON. TripleTransaction contains a list of TripleModel.

 [DataContract]
public class TripleTransaction
{
    [DataMember]
    public string Localization { get; set; }

    [DataMember]
    public List<TripleModel> Transactions { get; set; } 
}


 [DataContract]
   public class TripleModel
    {
        [DataMember]
        public int TransactionType { get; set; }

        [DataMember]
        public string SubjectLocalPart { get; set; }

        [DataMember]
        public string PredicateLocalPart { get; set; }

        [DataMember]
        public string ObjectPart { get; set; }

        [DataMember]
        public string Update { get; set; }

    }

The above datacontract is modeled like the POSTed JSON. Using FireBug I can see that the request looks correct. The JSON is in the message payload and the request header has a contentType: application/json

This is an example of the json that I am attempting to send:

 {
  "tripleTransaction": {
    "Localization": "en-us",
    "Transactions": [
      {
        "TransactionType": 0,
        "SubjectLocalPart": "testSubject",
        "PredicateLocalPart": "testPredicate",
        "ObjectPart": "1",
        "Update": "2"
      },
      {
        "TransactionType": 1,
        "SubjectLocalPart": "testSubject",
        "PredicateLocalPart": "testPredicate",
        "ObjectPart": "1"
      }
    ]
  }
}
1
  • KnownTypes? But i am not sure if it is needed on rest service Commented Mar 27, 2012 at 21:42

1 Answer 1

2

You should post this :

 {
    "Localization": "en-us",
    "Transactions": [
      {
        "TransactionType": 0,
        "SubjectLocalPart": "testSubject",
        "PredicateLocalPart": "testPredicate",
        "ObjectPart": "1",
        "Update": "2"
      },
      {
        "TransactionType": 1,
        "SubjectLocalPart": "testSubject",
        "PredicateLocalPart": "testPredicate",
        "ObjectPart": "1"
      }
    ]
  }

Under the POST variable name "tripleTransaction"

Otherwise it doesn't know to which parameter it should link your data

Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean by POST Variable name?
When your post data on a page, you can only post a list of key-value data. when I say "POST variable name" I mean the key. Please read en.wikipedia.org/wiki/POST_%28HTTP%29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.