0

I used to have a method in mvc application which can read the values from a post request. it was something like this.

[HttpPost]
public ActionResult ResponseFromExternalParty(FormCollection form)

and I was able to read the values as

var authCode = form["auth_code"];

now I need to do the same in another application which is not a mvc application, therefore I cannot use FormCollection object. I was told to use Stream object, so my new method looks like

 public void ResponseFromExternalParty(Stream form)

I cannot get my head around it as how to read post data from this Stream object. if I have to read querystring data I know I could have used something like

HttpUtility.ParseQueryString();

advance thanks for any help.

ahsant
  • 1,003
  • 4
  • 17
  • 25
  • Possible duplicate: http://stackoverflow.com/questions/20151556/how-to-get-the-http-post-data-in-c – Ryan Mar 24 '16 at 01:16
  • @Ryan no its not duplicate, that question is specifically for MVC. my problem is different. – ahsant Mar 24 '16 at 02:19
  • so are you using WebForms or WebApi or what? You still should have access to a `Request` object that has the `Form` data through the other libraries. – Ryan Mar 24 '16 at 02:38

1 Answers1

0

This answer is only for those guys who will be as confused as I was. Apparently ParseQueryString funtion can also read body of posted data. which is bit confusing as I thought ParseQueryString is only to Parse a query string into a NameValueCollection. So following is how to read posted data from Stream object.

   public void ResponseFromExternalParty(Stream form) {
   var requestBody = new StreamReader(form).ReadToEnd();
   var nameValueCollection = HttpUtility.ParseQueryString(requestBody);

   var authCode = nameValueCollection["auth_code"];
   }
ahsant
  • 1,003
  • 4
  • 17
  • 25