I'm trying to get the raw request body in ASP.NET 4.0 WebForm.
Request["param"], Request.Form["param"], Request.QueryString["param"] is not working.
Any idea of how to get this parameters?
// API POST Request with this Request Body (raw-json)
{
"name" : "Apple",
"image" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Red_Apple.jpg/265px-Red_Apple.jpg",
"price" : 35
}
// API Server trying to get request body.
// didn't work. empty
Request["name"]
// didn't work. empty
Request.Form["name"]
// didn't work. empty
Request.QueryString["name"]
You may use Request.InputStream
// include this in the top of your page to use JavaScriptSerializer and Hashtable
using System.Web.Script.Serialization;
using System.Collections;
...
using (var sr = new StreamReader(Request.InputStream))
{
string body = sr.ReadToEnd();
// Deserialize JSON to C# object
// you can use some modern libs such as Newtonsoft JSON.NET instead as well
JavaScriptSerializer serializer = new JavaScriptSerializer();
Hashtable hashtable = serializer.Deserialize<Hashtable>(body);
string name = hashtable["name"].ToString();
string image = hashtable["image"].ToString();
string price = hashtable["price"].ToString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.