0

I create json object and assign it to a StringContent of my HttpResponseMessage instance. Everything works fine when I call the Web API action, the result is 200, the content-length is how it should be, but how to find the content itself, where is the json? What I get in the browser and in Postman is this:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
  Content-Type: application/json
}

Why is this instead of my json string?

Content: System.Net.Http.StringContent

enter image description here

nmrlqa4
  • 659
  • 1
  • 9
  • 32
  • Possible duplicate of [C# Read (not write!) string from System.Net.Http.StringContent](https://stackoverflow.com/questions/36271702/c-sharp-read-not-write-string-from-system-net-http-stringcontent) – Aman B Oct 26 '18 at 09:42
  • where is the code that calls the API? – Hooman Bahreini Oct 26 '18 at 10:22

1 Answers1

0

If what you are trying to achieve is to return a valid JSON response, then this is the way to go in Asp.Net MVC

    public ActionResult HttpResponseMessage()
    {
        var oJSON = new { url = "path_to_file", hash = "aaaaaaaaaaaaaaaaa" };
        return Json(oJSON, JsonRequestBehavior.AllowGet);
    }

Response headers as seen by Postman:

Cache-Control →private
Content-Length →49
Content-Type →application/json; charset=utf-8
Date →Fri, 26 Oct 2018 13:31:44 GMT
Server →Microsoft-IIS/10.0
X-AspNet-Version →4.0.30319
X-AspNetMvc-Version →5.2
X-Powered-By →ASP.NET
X-SourceFiles →=?UTF-8?B?RTpcRXhhbSA3MCA0ODdcNzA0ODdcTVZDUm91dGVzXEhvbWVcSHR0cFJlc3BvbnNlTWVzc2FnZQ==?=

Response body as seen by Postman

{"url":"path_to_file","hash":"aaaaaaaaaaaaaaaaa"}
Alfredo A.
  • 1,697
  • 3
  • 30
  • 43