0

I am getting an exception while reading the post data. I get error on this line:

HttpContext.Current.Request.Form["UserID"].ToString();

And the error is :

System.Collections.Specialized.NameValueCollection.this[string].get returned null.

In method I have put this code :

StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream);
string requestFromPost = reader.ReadToEnd();

and data comes in that properly like this:

{
  "UserID": "1000",
  "Password": "ABCD"
}

Why I am not getting value in this HttpContext.Current.Request.Form["UserID"].ToString()? I also tried Request.QueryString but no success here. Where am I doing wrong? Any help or suggestion would be much appreciated. Thanks!

Dungeon
  • 972
  • 2
  • 16
  • 32
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – A Friend Oct 09 '18 at 12:30
  • @AFriend It is different from that link. – Dungeon Oct 09 '18 at 12:34

1 Answers1

3

There is no Form on this request. For a request body to be interpreted as form data, it must:

  • have a content type of x-www-form-urlencoded
  • be actually formatted as form encoded values, i.e. UserID=foo&Password=bar

JSON content is JSON, it will not be interpreted as form-data.

Web API should already take care of this for you. Given an action method:

public void Action(Credentials credentials)

where the Credentials class looks something like:

public class Credentials 
{
    string UserID { get; set;}
    string Password { get; set; }
}

You shouldn't have to do anything else to have the framework turn this incoming JSON data into an instance of Credentials and pass it to the action method. This is automatic unless you've done something strange that breaks the conventions that WebAPI expects.

Tom W
  • 5,108
  • 4
  • 30
  • 52
  • Thanks for your answer but how could I access the value coming in to my method? – Dungeon Oct 09 '18 at 12:37
  • 1
    Why are you reading directly from the request in a WebAPI project anyway? The model binder should create the action method arguments for you from the incoming request data. I suggest you read a WebAPI tutorial, there are thousands of them out there. – Tom W Oct 09 '18 at 12:39
  • Thanks @Tom. That really helped. – Dungeon Oct 09 '18 at 12:45
  • I did this using `JObject` but that was a wrong approach. I did it like this:`var obj = JObject.Parse(requestFromPost);` and then `obj["UserID"].ToString()`. – Dungeon Oct 09 '18 at 13:12
  • 1
    In WebAPI that's probably because the model binder has already read the request stream. Again, why are you doing this? WebAPI already knows how to read requests into objects. Using it the way it's designed to be used is the easiest way. – Tom W Oct 09 '18 at 13:21
  • I got your point @Tom. I was just saying that the my process is just hard and complex and seems to be a wrong approach. By the way Thanks! – Dungeon Oct 09 '18 at 13:23