Search code examples
c#restapiweb-serviceshttp-status-codes

Why am I getting response status code 204 No Content in my WebApi?


First of all, my English is not so good, I hope you understand what I am trying to say.

I'm new with WebApi's. I created one and all seems good. In my function I return a string, but when I test the web api with Postman it returns a status code 204 no content. I looked for this status and apparently my request was succeded, but when I look at my database nothing happend. (my function saves some data in my database and returns a string when it is succeded or not).

So, my question is, Why am I receiving this status code when my function must to return a string? Or why when I test the Web Api with Postman it returns a 204 No Content code (that means my request was succeded) when is not true (because nothing was saved in my database)?

Here is my function in my web api:


    [HttpPost]
    public string IncomingPO(string request)
    {
      //do some stuff
      return "response as a string";
    }

My parameter "string request" is a string that cointains a xml. I read the string as a xml and do some stuff with it. (save some nodes from the xml in a database, for example).

If all the function is succeded or not I expect the output of my string, but I'm no receiving anything. Only the status code 204 No Content. (when content should really be returned). Any suggestion is welcome.


Solution

  • It can solve my problem. The only thing I had to do was specify the route with which my Api website will be invoked. In addition I also had to modify the data type of my parameter and my function.

    Something like this:

    [HttpPost]
    [Route("api/Controller/IncomingPO")]
    public HttpResponseMessage IncomingPO(HttpRequestMessage request)
    {
      //do some stuff
      return new HttpResponseMessage()
                {
                    Content = new StringContent("My response string", System.Text.Encoding.UTF8, "application/xml"),
                    StatusCode = HttpStatusCode.OK
                };
    }