Search code examples
c#asp.netasp.net-mvcpostasp.net-web-api

POST string to ASP.NET Web Api application - returns null


Im trying to transmit a string from client to ASP.NET MVC4 application.

But I can not receive the string, either it is null or the post method can not be found (404 error)

Client Code to transmit the string (Console Application):

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:49032/api/test");
request.Credentials = new NetworkCredential("user", "pw");
request.Method = "POST";
string postData = "Short test...";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;

Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();

StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
Console.ReadLine();

ASP.NET Web Api Controller:

public class TestController : ApiController
{
    [Authorize]
    public String Post(byte[] value)
    {
        return value.Length.ToString();
    }
}

In that case I'm able to call the "Post" method, but "value" is NULL. If I change the method signature to (string value) than it will never called.

Even "without" the [Authorize] setting it has the same strange behavior. -> So it has nothing to do with the user authentication.

Any ideas what I'm doing wrong? I'm grateful for any help.


Solution

  • You seem to have used some [Authorize] attribute on your Web API controller action and I don't see how this is relevant to your question.

    So, let's get into practice. Here's a how a trivial Web API controller might look like:

    public class TestController : ApiController
    {
        public string Post([FromBody] string value)
        {
            return value;
        }
    }
    

    and a consumer for that matter:

    class Program
    {
        static void Main()
        {
            using (var client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                var data = "=Short test...";
                var result = client.UploadString("http://localhost:52996/api/test", "POST", data);
                Console.WriteLine(result);
            }
        }
    }
    

    You will undoubtedly notice the [FromBody] decoration of the Web API controller attribute as well as the = prefix of the POST data on the client side. I would recommend you reading about how does the Web API does parameter binding to better understand the concepts.

    As far as the [Authorize] attribute is concerned, this could be used to protect some actions on your server from being accessible only to authenticated users. Actually it is pretty unclear what you are trying to achieve here.You should have made this more clear in your question by the way. Are you are trying to understand how parameter bind works in ASP.NET Web API (please read the article I've linked to if this is your goal) or are attempting to do some authentication and/or authorization? If the second is your case you might find the following post that I wrote on this topic interesting to get you started.

    And if after reading the materials I've linked to, you are like me and say to yourself, WTF man, all I need to do is POST a string to a server side endpoint and I need to do all of this? No way. Then checkout ServiceStack. You will have a good base for comparison with Web API. I don't know what the dudes at Microsoft were thinking about when designing the Web API, but come on, seriously, we should have separate base controllers for our HTML (think Razor) and REST stuff? This cannot be serious.