-1

I am making a rest call in which I get a HttpWebResponse that contains data. It seems the data is serialized, and I am trying to get the plain text of the request. I have been using the chrome extension Advanced Rest client, which when calling the same request it is able to display the text version of the json response.

From what I have read on here, you are required to deserialize into the expected object. However, it is pretty clear that chrome plugin has no idea about the object type and can still print out plain text.

Is it possible to do the same in c#?

HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

request.Method = "POST";
request.ContentType = "application/json";

// [code removed for setting json data via stream writer


using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{

   // This is where I am trying to figure out how to get plain readable text out of response.GetResponseStream()

}

Edit: If I simply use a StreamReader to get the text from the response stream, I get a bunch of binary data and not the plain json text.

Edit: realized the problem had to do with compression. This can be closed.

JeremyK
  • 1,075
  • 1
  • 22
  • 45
  • Possible duplicate of [JSONResult to String](http://stackoverflow.com/questions/4571985/jsonresult-to-string) – MethodMan Apr 28 '16 at 18:37
  • I'm voting to close this question as off-topic because I was way off on what the problem was. I do not believe this will be helpful to future people stumbling upon it. – JeremyK Apr 28 '16 at 19:13
  • then just delete the question – MethodMan Apr 28 '16 at 19:55

2 Answers2

3

I'm not sure if got it right, but you can get the response as a string doing this:

using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
}
cristianorbs
  • 670
  • 3
  • 9
  • 16
  • I get what appears to be serialized binary data when I do that. – JeremyK Apr 28 '16 at 18:44
  • This answer could be helpful. Have you checked it already? http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object/3806407#3806407 – cristianorbs Apr 28 '16 at 18:56
  • Figured it out, realized the header included gzip as the encoding. Had to uncompress it. – JeremyK Apr 28 '16 at 19:09
1

Turned out my problem was due to compression. I realized the header contained "Content-Encoding: gzip" so I searched on how to unzip with gzip compression and then the text was proper json. Thanks all

JeremyK
  • 1,075
  • 1
  • 22
  • 45
  • Could you mark this answer as accepted please? Also for future readers: http://stackoverflow.com/questions/839888/httpwebrequest-native-gzip-compression – Rob Apr 29 '16 at 00:23
  • 1
    @rob was going to once the 2 day requirement passes. I'll update with the actual code tomorrow when I get to work. – JeremyK Apr 29 '16 at 01:16