How to pass and use JSON parameter for WCF RESTful service?

I am starting to use RESTful services.

I need to create an interface where the client must pass up to 9 parameters.

I would prefer to pass parameters as a JSON object.

For example, if my JSON is:

'{ "age":100, "name":"foo", "messages":["msg 1","msg 2","msg 3"], "favoriteColor" : "blue", "petName" : "Godzilla", "IQ" : "QuiteLow" }' 

And if I need to execute the server side method below at the end:

 public Person FindPerson(Peron lookUpPerson) { Person found = null; // Implementation that finds the Person and sets 'found' return found; } 

Question (s):
How can I make a call from the client side with the above JSON string? And how can I create a signature and implementation of a RESTful service method that

  • accepts this json
  • parses and deserializes it into a Person object and
  • calls / returns the returned FindPerson method back to the client?
+7
source share
2 answers

If you want to create a WCF operation to receive this JSON input, you will need to define a data contract that will map to this input. There are several tools that do this automatically, including the one I wrote a while ago at http://jsontodatacontract.azurewebsites.net/ (more about how this tool was written on in this blog post ). The tool generated this class, which you can use:

 // Type created for JSON at <<root>> [System.Runtime.Serialization.DataContractAttribute()] public partial class Person { [System.Runtime.Serialization.DataMemberAttribute()] public int age; [System.Runtime.Serialization.DataMemberAttribute()] public string name; [System.Runtime.Serialization.DataMemberAttribute()] public string[] messages; [System.Runtime.Serialization.DataMemberAttribute()] public string favoriteColor; [System.Runtime.Serialization.DataMemberAttribute()] public string petName; [System.Runtime.Serialization.DataMemberAttribute()] public string IQ; } 

Then you need to define an operation contract to get this. Since JSON needs to go into the request body, the most natural HTTP method to use is POST , so you can define the operation as shown below: the "POST" method and the "Bare" style (which means that your JSON directly displays the parameter). Note that you can even omit the Method and BodyStyle , since "POST" and WebMessageBodyStyle.Bare are their default values, respectively).

 [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)] public Person FindPerson(Peron lookUpPerson) { Person found = null; // Implementation that finds the Person and sets 'found' return found; } 

Now in the method you have the input mapped to lookupPerson . How you implement the logic of your method is up to you.

Update after comment

One example of calling a service using JavaScript (via jQuery) can be found below.

 var input = '{ "age":100, "name":"foo", "messages":["msg 1","msg 2","msg 3"], "favoriteColor" : "blue", "petName" : "Godzilla", "IQ" : "QuiteLow" }'; var endpointAddress = "http://your.server.com/app/service.svc"; var url = endpointAddress + "/FindPerson"; $.ajax({ type: 'POST', url: url, contentType: 'application/json', data: input, success: function(result) { alert(JSON.stringify(result)); } }); 
+9
source

1-Add WebGet attribute

 <OperationContract()> _ <WebGet(UriTemplate:="YourFunc?inpt={inpt}", BodyStyle:=WebMessageBodyStyle.Wrapped, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Xml)> _ Public Function YourFunch(inpt As String) As String 

2-Use NewtonSoft to serialize / deserialize json into your object (note that this is just taken in String), NewtonSoft is much faster than MS serializer.

use NewtonSoft to serialize http://json.codeplex.com/

3- your .svc file will contain Factory = "System.ServiceModel.Activation.WebServiceHostFactory

4- your web.config file will contain

  <behaviors> <endpointBehaviors> <behavior name="webHttpBehavior"> <webHttp /> </behavior> </endpointBehaviors> </behaviors> 

... and ...

  <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> 
+1
source

All Articles