View Http response content in ServiceStack

I am using ServiceStack to create a C # client in a JSON RESTful service. I have this code that my DTO returns:

Search Result = restClient.Get (search);

This works fine, but in order to efficiently debug search results that return back, I need to output the text content from the underlying HTTP Response object. (I do not yet know all the response elements to add them to the DTO).

Is there any way to get the basic HTTP response and therefore the full text content from my result object?

Thanks in advance. @adamfowleruk

+4
source share
4 answers

When inheriting from the ServiceStack service's built-in service , you can access the basic request and response directly from the Response class with:

public class MyService : Service { public object Get(Request request) { base.Request ... base.Response ... } } 

You will not see the answer in your service or filters, since it is directly recorded in the response stream and is the last thing that ServiceStack does after execution of your service and all response filters .

For HTTP diagnostics, I recommend using Fiddler or the WebInspector also built into ServiceStack ServiceStack may also help.

Using ServiceStack

If you use C # Service Clients , you can just ask what you want, for example. you can access the returned answer as a raw string:

 string responseJson = client.Get<string>("/poco/World"); 

Or as raw bytes:

 byte[] responseBytes = client.Get<byte[]>("/poco/World"); 

Or as a stream:

 using (Stream responseStream = client.Get<Stream>("/poco/World")) { var dto = responseStream.ReadFully().FromUtf8Bytes().FromJson<PocoResponse>(); } 

Or even access the populated HttpWebResponse object:

 HttpWebResponse webResponse = client.Get<HttpWebResponse>("/poco/World"); webResponse.Headers["X-Response"] //World using (webResponse) using (var stream = webResponse.GetResponseStream()) using (var sr = new StreamReader(stream)) { string response = sr.ReadToEnd(); } 

You can also explore HttpWebResponse using Global and Local Response filters, for example:

 JsonServiceClient.HttpWebResponseFilter = httpRes => { .. }; 

Or using a local filter:

 var client = new JsonServiceClient(baseUrl) { ResponseFilter = httpRes => { .. } }; 

Third party service consumption

If you are using a third-party REST / HTTP API, you can use responseFilter: in the ServiceStack HTTP Util extensions :

 List<GithubRepo> repos = "https://api.github.com/users/{0}/repos".Fmt(user) .GetJsonFromUrl(responseFilter: httpRes => { var remaining = httpRes.Headers["X-Api-Remaining"]; }) .FromJson<List<GithubRepo>>(); 
+7
source

I use Fiddler to debug my services. It provides you with all sorts of cool HTTP debugging tools.

http://www.fiddler2.com/fiddler2/

+3
source

I like to use RestConsole. This is a Chrome extension, and you can easily send POST requests and see the response. It is also convenient to create data samples, and then enter the ServiceStack code and see what happens. The ServiceStack PluralSight course has a good demonstration of how to use them.

+3
source

Thanks to the above help, I found the correct answer. Documentation here for others: -

  SearchResponse result = null; // my ServiceStack DTO HttpWebResponse webResponse = restClient.Get<HttpWebResponse>( completePath("/v1/search",qp)); // builds the URL with parameters using (var stream = webResponse.GetResponseStream()) using (var sr = new StreamReader(stream)) { var text = sr.ReadToEnd(); log.log ("response text: " + text); // *** PRINTING STRING VALUE HERE FOR DEBUG result = text.FromJson<SearchResponse>(); } // Now do something useful with the result DTO object log.log ("RESULT: " + result.ToString ()); for (int i = 0; i < result.Results.Length; i++) { log.log ("Result " + i + ": " + result.Results[i].ToString()); } 
+2
source

All Articles