Can I post JSON/XML string as a POST request to REST ServiceStack?
I am currently working on ServiceStack
, just very new to it. (Quite interesting; they say they are better than WCF and REST WCF (GET,POST,PUT,DELETE))
I also found it helpful, with less code to do and run it fast.
Currently when I want to post the data. I make object of class and send in POST request.
JsonServiceClient client = new JsonServiceClient(myURL);
MYclass cls= MakeObjectForServiceStackToInsertData();
var res = client.Post<MYClass>("/postrequest", cls);
By above code you can understand, what is the thing I am doing. I guess I am not wrong. Please let me know if you are confused.
Can I make a string in JSON for my class (KEY->Value)
in my client application (manually) and POST it using service stack to server to Send Data.
ie
string str = myJsonString();
var res = client.Post<.....>
So, instead of posting the whole object, it is better if I can make JSON string and deserialize it in POST event of ServiceStack and insert data in DB?
Any idea?
I'm not sure of what your motive is of wanting to pass in a raw JSON string is, but if it's to ensure you only pass in a small JSON payload you can confidently use the Json/Jsv ServiceClients since they don't include null values so will only pass the fields you populate.
You can verify what gets serilalized by using the Json serializer directly.
Console.WriteLine(JsonSerializer.SerializeToString(cls));
If you just want to test/debug your service, the best way is to simply use the browser by either populating the fields with query string, ie:
http://localhost/myservice/postrequest?Field1=value1&Field2=value2
In most cases where you want to pass in a raw JSON string you will need to use another HTTP client, in which case you're better off calling the web services POST'ing form data instead since it's natively supported by all HTTP clients and you don't need a JSON serializer to do. eg here's how to call the same service, via HTTP POST with curl:
curl -d "Field1=value1&Field2=value2" http://localhost/myservice/postrequest
There are many other ways you can call the same service, see Service Stack's Hello World example for the full list. Here's how you would call it using HTML:
<form action="http://localhost/myservice/postrequest" method="POST">
<input type="text" name="Field1" value="value1" />
<input type="text" name="Field2" value="value2" />
</form>
Since you're posting it from a web browser Service Stack will return HTML but if you want to see the JSON result you just need to append ?format=json to the url.
Or you can simply use JavaScript and jQuery to POST to your web service, the example below will return the results as JSON which get's automatically converted into a JS object:
$.ajax({
type: 'POST',
url: "http://localhost/myservice/postrequest",
data: {Field1: "value1", Field2: "value2"},
success: function(r) { alert(r.Result) },
dataType: "application/json"
});
The nice thing about using a web browser to debug your services is that you can use the built-in network inspector (ie web inspector in Chrome) to view the complete request/response of your web service.
If you're not familiar with it, I also recommend getting familiar with Fiddler which allows you to easily analyze HTTP traffic.
According to my knowledge you need to pass object (DTO request). But you can make post request with json , if you want example to post json request then look at https://github.com/ServiceStack/ServiceStack.Extras/blob/master/doc/UsageExamples/UsingRestAndJson.cs
链接地址: http://www.djcxy.com/p/68704.html