Why does HttpResponseMessage not show its content?












0














Hello i am trying to send a Json object using the HttpResponseMessage .
Even though while debugging the data looks like it was inserted in the Content section (104 bytes present) when using Postman when retrieveing the json , in the Content section there is no data,just a header.



JsonResponse



{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"text/plain; charset=utf-8"
]
} ////why no data ??
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": ,
"requestMessage": null,
"isSuccessStatusCode": true
}


As you can see there is no content.I am reusing the same code as in an earlier application and i did not get this problem.Why is the content empty?



Code



private static List<User> users = new List<User> {
new User{ Id = 0, Age = 0, Name = "Failed"},
new User{ Id = 12, Age = 33, Name = "Daniel"},
new User{ Id = 13, Age = 33, Name = "Marian"},
};


[HttpGet]
[Route("/api/getusers")]
public async Task<HttpResponseMessage> GetUsers() {
await Task.Delay(1000);
var str = JsonConvert.SerializeObject(users);
return new HttpResponseMessage {
StatusCode = HttpStatusCode.OK,
Content = new StringContent(str, Encoding.UTF8)
};
}









share|improve this question





























    0














    Hello i am trying to send a Json object using the HttpResponseMessage .
    Even though while debugging the data looks like it was inserted in the Content section (104 bytes present) when using Postman when retrieveing the json , in the Content section there is no data,just a header.



    JsonResponse



    {
    "version": {
    "major": 1,
    "minor": 1,
    "build": -1,
    "revision": -1,
    "majorRevision": -1,
    "minorRevision": -1
    },
    "content": {
    "headers": [
    {
    "key": "Content-Type",
    "value": [
    "text/plain; charset=utf-8"
    ]
    } ////why no data ??
    ]
    },
    "statusCode": 200,
    "reasonPhrase": "OK",
    "headers": ,
    "requestMessage": null,
    "isSuccessStatusCode": true
    }


    As you can see there is no content.I am reusing the same code as in an earlier application and i did not get this problem.Why is the content empty?



    Code



    private static List<User> users = new List<User> {
    new User{ Id = 0, Age = 0, Name = "Failed"},
    new User{ Id = 12, Age = 33, Name = "Daniel"},
    new User{ Id = 13, Age = 33, Name = "Marian"},
    };


    [HttpGet]
    [Route("/api/getusers")]
    public async Task<HttpResponseMessage> GetUsers() {
    await Task.Delay(1000);
    var str = JsonConvert.SerializeObject(users);
    return new HttpResponseMessage {
    StatusCode = HttpStatusCode.OK,
    Content = new StringContent(str, Encoding.UTF8)
    };
    }









    share|improve this question



























      0












      0








      0







      Hello i am trying to send a Json object using the HttpResponseMessage .
      Even though while debugging the data looks like it was inserted in the Content section (104 bytes present) when using Postman when retrieveing the json , in the Content section there is no data,just a header.



      JsonResponse



      {
      "version": {
      "major": 1,
      "minor": 1,
      "build": -1,
      "revision": -1,
      "majorRevision": -1,
      "minorRevision": -1
      },
      "content": {
      "headers": [
      {
      "key": "Content-Type",
      "value": [
      "text/plain; charset=utf-8"
      ]
      } ////why no data ??
      ]
      },
      "statusCode": 200,
      "reasonPhrase": "OK",
      "headers": ,
      "requestMessage": null,
      "isSuccessStatusCode": true
      }


      As you can see there is no content.I am reusing the same code as in an earlier application and i did not get this problem.Why is the content empty?



      Code



      private static List<User> users = new List<User> {
      new User{ Id = 0, Age = 0, Name = "Failed"},
      new User{ Id = 12, Age = 33, Name = "Daniel"},
      new User{ Id = 13, Age = 33, Name = "Marian"},
      };


      [HttpGet]
      [Route("/api/getusers")]
      public async Task<HttpResponseMessage> GetUsers() {
      await Task.Delay(1000);
      var str = JsonConvert.SerializeObject(users);
      return new HttpResponseMessage {
      StatusCode = HttpStatusCode.OK,
      Content = new StringContent(str, Encoding.UTF8)
      };
      }









      share|improve this question















      Hello i am trying to send a Json object using the HttpResponseMessage .
      Even though while debugging the data looks like it was inserted in the Content section (104 bytes present) when using Postman when retrieveing the json , in the Content section there is no data,just a header.



      JsonResponse



      {
      "version": {
      "major": 1,
      "minor": 1,
      "build": -1,
      "revision": -1,
      "majorRevision": -1,
      "minorRevision": -1
      },
      "content": {
      "headers": [
      {
      "key": "Content-Type",
      "value": [
      "text/plain; charset=utf-8"
      ]
      } ////why no data ??
      ]
      },
      "statusCode": 200,
      "reasonPhrase": "OK",
      "headers": ,
      "requestMessage": null,
      "isSuccessStatusCode": true
      }


      As you can see there is no content.I am reusing the same code as in an earlier application and i did not get this problem.Why is the content empty?



      Code



      private static List<User> users = new List<User> {
      new User{ Id = 0, Age = 0, Name = "Failed"},
      new User{ Id = 12, Age = 33, Name = "Daniel"},
      new User{ Id = 13, Age = 33, Name = "Marian"},
      };


      [HttpGet]
      [Route("/api/getusers")]
      public async Task<HttpResponseMessage> GetUsers() {
      await Task.Delay(1000);
      var str = JsonConvert.SerializeObject(users);
      return new HttpResponseMessage {
      StatusCode = HttpStatusCode.OK,
      Content = new StringContent(str, Encoding.UTF8)
      };
      }






      c# asp.net-core json.net httpresponse






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 9:02









      Kirk Larkin

      19.9k33756




      19.9k33756










      asked Nov 13 '18 at 8:47









      Bercovici AdrianBercovici Adrian

      1,0751816




      1,0751816
























          1 Answer
          1






          active

          oldest

          votes


















          2














          ASP.NET Core does not have built-in support for returning HttpResponseMessage. If you want to return JSON, you can use IActionResult and let ASP.NET Core handle the serialisation for you. Here's an updated example:



          public async Task<IActionResult> GetUsers() {
          await Task.Delay(1000);
          return Json(users);
          }


          There are more options here, for situations where you want to allow content-negotiation, for example. Here's a version that supports content-negotiation using OkObjectResult:



          public async Task<IActionResult> GetUsers() {
          await Task.Delay(1000);
          return Ok(users);
          }


          You can even just return users itself if you prefer:



          public async Task<List<User>> GetUsers() {
          await Task.Delay(1000);
          return users;
          }


          That should be enough to get you started - the official documentation explains things further: Controller action return types in ASP.NET Core Web API.






          share|improve this answer























          • I wanted to return a StatusCode too.So i will need to create a wrapper around my object and the StatusCode.
            – Bercovici Adrian
            Nov 13 '18 at 8:59






          • 1




            If you mean a HTTP StatusCode, the approach I've shown will set it to 200 automatically. If you mean a custom StatusCode that's not HTTP-based, you will want to create your own wrapper class rather than using HttpResponseMessage. If that's the case, let me know and I'll provide an updated example.
            – Kirk Larkin
            Nov 13 '18 at 9:01












          • No your solution is fine..i didn't understand that it included the status code.
            – Bercovici Adrian
            Nov 13 '18 at 9:04











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277049%2fwhy-does-httpresponsemessage-not-show-its-content%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          ASP.NET Core does not have built-in support for returning HttpResponseMessage. If you want to return JSON, you can use IActionResult and let ASP.NET Core handle the serialisation for you. Here's an updated example:



          public async Task<IActionResult> GetUsers() {
          await Task.Delay(1000);
          return Json(users);
          }


          There are more options here, for situations where you want to allow content-negotiation, for example. Here's a version that supports content-negotiation using OkObjectResult:



          public async Task<IActionResult> GetUsers() {
          await Task.Delay(1000);
          return Ok(users);
          }


          You can even just return users itself if you prefer:



          public async Task<List<User>> GetUsers() {
          await Task.Delay(1000);
          return users;
          }


          That should be enough to get you started - the official documentation explains things further: Controller action return types in ASP.NET Core Web API.






          share|improve this answer























          • I wanted to return a StatusCode too.So i will need to create a wrapper around my object and the StatusCode.
            – Bercovici Adrian
            Nov 13 '18 at 8:59






          • 1




            If you mean a HTTP StatusCode, the approach I've shown will set it to 200 automatically. If you mean a custom StatusCode that's not HTTP-based, you will want to create your own wrapper class rather than using HttpResponseMessage. If that's the case, let me know and I'll provide an updated example.
            – Kirk Larkin
            Nov 13 '18 at 9:01












          • No your solution is fine..i didn't understand that it included the status code.
            – Bercovici Adrian
            Nov 13 '18 at 9:04
















          2














          ASP.NET Core does not have built-in support for returning HttpResponseMessage. If you want to return JSON, you can use IActionResult and let ASP.NET Core handle the serialisation for you. Here's an updated example:



          public async Task<IActionResult> GetUsers() {
          await Task.Delay(1000);
          return Json(users);
          }


          There are more options here, for situations where you want to allow content-negotiation, for example. Here's a version that supports content-negotiation using OkObjectResult:



          public async Task<IActionResult> GetUsers() {
          await Task.Delay(1000);
          return Ok(users);
          }


          You can even just return users itself if you prefer:



          public async Task<List<User>> GetUsers() {
          await Task.Delay(1000);
          return users;
          }


          That should be enough to get you started - the official documentation explains things further: Controller action return types in ASP.NET Core Web API.






          share|improve this answer























          • I wanted to return a StatusCode too.So i will need to create a wrapper around my object and the StatusCode.
            – Bercovici Adrian
            Nov 13 '18 at 8:59






          • 1




            If you mean a HTTP StatusCode, the approach I've shown will set it to 200 automatically. If you mean a custom StatusCode that's not HTTP-based, you will want to create your own wrapper class rather than using HttpResponseMessage. If that's the case, let me know and I'll provide an updated example.
            – Kirk Larkin
            Nov 13 '18 at 9:01












          • No your solution is fine..i didn't understand that it included the status code.
            – Bercovici Adrian
            Nov 13 '18 at 9:04














          2












          2








          2






          ASP.NET Core does not have built-in support for returning HttpResponseMessage. If you want to return JSON, you can use IActionResult and let ASP.NET Core handle the serialisation for you. Here's an updated example:



          public async Task<IActionResult> GetUsers() {
          await Task.Delay(1000);
          return Json(users);
          }


          There are more options here, for situations where you want to allow content-negotiation, for example. Here's a version that supports content-negotiation using OkObjectResult:



          public async Task<IActionResult> GetUsers() {
          await Task.Delay(1000);
          return Ok(users);
          }


          You can even just return users itself if you prefer:



          public async Task<List<User>> GetUsers() {
          await Task.Delay(1000);
          return users;
          }


          That should be enough to get you started - the official documentation explains things further: Controller action return types in ASP.NET Core Web API.






          share|improve this answer














          ASP.NET Core does not have built-in support for returning HttpResponseMessage. If you want to return JSON, you can use IActionResult and let ASP.NET Core handle the serialisation for you. Here's an updated example:



          public async Task<IActionResult> GetUsers() {
          await Task.Delay(1000);
          return Json(users);
          }


          There are more options here, for situations where you want to allow content-negotiation, for example. Here's a version that supports content-negotiation using OkObjectResult:



          public async Task<IActionResult> GetUsers() {
          await Task.Delay(1000);
          return Ok(users);
          }


          You can even just return users itself if you prefer:



          public async Task<List<User>> GetUsers() {
          await Task.Delay(1000);
          return users;
          }


          That should be enough to get you started - the official documentation explains things further: Controller action return types in ASP.NET Core Web API.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 9:02

























          answered Nov 13 '18 at 8:58









          Kirk LarkinKirk Larkin

          19.9k33756




          19.9k33756












          • I wanted to return a StatusCode too.So i will need to create a wrapper around my object and the StatusCode.
            – Bercovici Adrian
            Nov 13 '18 at 8:59






          • 1




            If you mean a HTTP StatusCode, the approach I've shown will set it to 200 automatically. If you mean a custom StatusCode that's not HTTP-based, you will want to create your own wrapper class rather than using HttpResponseMessage. If that's the case, let me know and I'll provide an updated example.
            – Kirk Larkin
            Nov 13 '18 at 9:01












          • No your solution is fine..i didn't understand that it included the status code.
            – Bercovici Adrian
            Nov 13 '18 at 9:04


















          • I wanted to return a StatusCode too.So i will need to create a wrapper around my object and the StatusCode.
            – Bercovici Adrian
            Nov 13 '18 at 8:59






          • 1




            If you mean a HTTP StatusCode, the approach I've shown will set it to 200 automatically. If you mean a custom StatusCode that's not HTTP-based, you will want to create your own wrapper class rather than using HttpResponseMessage. If that's the case, let me know and I'll provide an updated example.
            – Kirk Larkin
            Nov 13 '18 at 9:01












          • No your solution is fine..i didn't understand that it included the status code.
            – Bercovici Adrian
            Nov 13 '18 at 9:04
















          I wanted to return a StatusCode too.So i will need to create a wrapper around my object and the StatusCode.
          – Bercovici Adrian
          Nov 13 '18 at 8:59




          I wanted to return a StatusCode too.So i will need to create a wrapper around my object and the StatusCode.
          – Bercovici Adrian
          Nov 13 '18 at 8:59




          1




          1




          If you mean a HTTP StatusCode, the approach I've shown will set it to 200 automatically. If you mean a custom StatusCode that's not HTTP-based, you will want to create your own wrapper class rather than using HttpResponseMessage. If that's the case, let me know and I'll provide an updated example.
          – Kirk Larkin
          Nov 13 '18 at 9:01






          If you mean a HTTP StatusCode, the approach I've shown will set it to 200 automatically. If you mean a custom StatusCode that's not HTTP-based, you will want to create your own wrapper class rather than using HttpResponseMessage. If that's the case, let me know and I'll provide an updated example.
          – Kirk Larkin
          Nov 13 '18 at 9:01














          No your solution is fine..i didn't understand that it included the status code.
          – Bercovici Adrian
          Nov 13 '18 at 9:04




          No your solution is fine..i didn't understand that it included the status code.
          – Bercovici Adrian
          Nov 13 '18 at 9:04


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277049%2fwhy-does-httpresponsemessage-not-show-its-content%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python