4

I found you are the only one who answers for service stack, I don't have emails and what ever you provided for last questions to me, seems fine.

I have seen your profile and as you are the main founder of mythZ, i seems to ask you my question again.

For one of my question, POST data in JSON format to Service stack, i appreciate your answer. your answer is right, however in my case i am having following case.Let me describe in more detail.

I have seen "Hello World" example of service stack. I got link for https://github.com/ServiceStack/ServiceStack.Extras/blob/master/doc/UsageExamples/UsingRestAndJson.cs

In my case, I am having console application which calls service stack (which inserts data in DB) Now, in that console application, i have made one class (class1) which is there in service stack with the same properties.

I assign values to properties of that class in my console application and POST the whole object to service stack. Syntex is like below

 JsonServiceClient client = new JsonServiceClient(myURL);
       MYclass cls= MakeObjectForServiceStackToInsertData();
        var res = client.Post<MYClass>("/postrequest", cls); 

I have use POST as above. which seems okay. at the service stack end in OnPOST event, i get this data and insert in DB. It is working fine for me.

Now my client, wants that we need to pass data in any format. JSON/XML. I know it is possible as you provide me a "Hello world" example link, it is mention over there.

But all i found is, they have used ajax/Jquery to post data to service. In my case this is console application, so i am not able to use ajax/Jquery. I am wondering that, is it possible to pass data in JSON format and do operation in my case.

Thank you very much in advance.

amit patel
  • 2,287
  • 8
  • 31
  • 45

1 Answers1

6

So if you want to post any untyped and free-text JSON or XML to ServiceStack then you wont be able to ServiceStack's generic typed C# clients (i.e. its JsonServiceClient, XmlServiceClient, etc). Instead you just need to use any basic Http Client like the HttpWebRequest that comes with .NET.

As I've mentioned earlier sending free-text json or xml is not the normal way to call ServiceStack web services (i.e. it's recommended to use typed DTOs and one of the generic service client), but since you've asked here are stand-alone, dependency-free examples of how to call ServiceStack's Hello World example web service:

Sending free-text JSON

const string RemoteUrl = "http://www.servicestack.net/ServiceStack.Hello/servicestack/hello";

var httpReq = (HttpWebRequest)WebRequest.Create(RemoteUrl);
httpReq.Method = "POST";
httpReq.ContentType = httpReq.Accept = "application/json";

using (var stream = httpReq.GetRequestStream())
using (var sw = new StreamWriter(stream))
{
    sw.Write("{\"Name\":\"World!\"}");
}

using (var response = httpReq.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
    Assert.That(reader.ReadToEnd(), Is.EqualTo("{\"Result\":\"Hello, World!\"}"));
}

Sending free-text XML

var httpReq = (HttpWebRequest)WebRequest.Create(RemoteUrl);
httpReq.Method = "POST";
httpReq.ContentType = httpReq.Accept = "application/xml";

using (var stream = httpReq.GetRequestStream())
using (var sw = new StreamWriter(stream))
{
    sw.Write("<Hello xmlns=\"http://schemas.servicestack.net/types\"><Name>World!</Name></Hello>");
}

using (var response = httpReq.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
    Assert.That(reader.ReadToEnd(), Is.EqualTo("<HelloResponse xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.servicestack.net/types\"><Result>Hello, World!</Result></HelloResponse>"));
}

I have added the above examples into this Runnable Unit Test.

I recommend getting familiar with a HTTP traffic analyzer tool so you can easily see the HTTP traffic that is sent and received to and from your web service. From then on, being able to workout how to call your service becomes trivial. Some great HTTP traffic analyzers include:

  • Fiddler
  • Network inspectors in Browser (e.g. Chrome, Safari, Firefox and IE have great tools)
  • WireShark
mythz
  • 141,670
  • 29
  • 246
  • 390
  • 1
    thanks mythz, ultimately the solution is using HTTPWebRequest in console application to make JSON string and pass to service stack and de-serialize there and do insert operation. or........... pass create an object in console and assign properies value in console application and then pass the whole object as DTO to service stack and do insert operation – amit patel Nov 08 '11 at 07:02
  • what do you suggest about above????? which is better, because JSON will create a long string and must need to KEY name similar to Properites to map with object while de- serialize on service stack side. – amit patel Nov 08 '11 at 07:04
  • 2
    So I definitely recommend using the same typed DTOs at both ends. A typed API makes it easy to code against, iteratively develop and refactor. In most cases you just drop the .dll and you have the latest changes, if something is broken (i.e. the client is relying on a removed field) you know about it at compile time, much better. As for serializers I recommend JSON, ServiceStack's JsonSerializer is much faster and more resilient to DTO changes. – mythz Nov 08 '11 at 07:11