I am attempting to read some data for a POST request made to a URL i control.
The data is sent as JSON in the POST request body so i created a regular Handler and use HttpContext, i dont seem to find any option to read this data (I have tried .Form).
public partial class _Default : Page
{
public class Movie
{
public string Name { get; set; }
public int Year { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if(Request.HttpMethod.ToUpper() == "POST")
{
Response.Clear();
Stream stream = Request.InputStream;
StreamReader sr = new StreamReader(stream);
JsonSerializer serializer = new JsonSerializer();
Movie movie = (Movie)serializer.Deserialize(sr, typeof(Movie));
Response.Write(movie.Name);
Response.ContentType = "text/html; charset=UTF-8";
Response.End();
}
}
}
The code posted in my original post deserialized an HTTP JSON stream into a C# type.
{
"Name": "Star Wars",
"Year": 1977
}
I tested the code before posting.
It's not clear why you are converting the JSON stream to a string, ReadToEnd(), then deserializing the string. I cannot see your code, type, or JSON so I have no way to reproduce the issue.
Did you, at least, try the code I posted?
Maybe this is what to want?
if(Request.HttpMethod.ToUpper() == "POST")
{
Response.Clear();
string json = new StreamReader(Request.InputStream).ReadToEnd();
Movie movie = JsonConvert.DeserializeObject<Movie>(json);
Response.Write(movie.Name);
Response.ContentType = "text/html; charset=UTF-8";
Response.End();
}
Yes I tried the code you posted. What was originally happening was the stream was empty/null. It's with trial and error that I got the data to show in debug mode.
I think what you have posted may do the trick. Let me give it a shot. Thanks
Member
288 Points
1286 Posts
Read POST data in request body
Dec 12, 2019 01:16 PM|uid250511|LINK
I am attempting to read some data for a POST request made to a URL i control.
The data is sent as JSON in the POST request body so i created a regular Handler and use HttpContext, i dont seem to find any option to read this data (I have tried .Form).
Is there another way around this?
All-Star
53011 Points
23596 Posts
Re: Read POST data in request body
Dec 12, 2019 02:39 PM|mgebhard|LINK
Read the input stream. Assuming Web Forms...
https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm
Member
288 Points
1286 Posts
Re: Read POST data in request body
Dec 12, 2019 05:08 PM|uid250511|LINK
Thanks, thats almost got me back on track.
If i have
then i see the data, however i cant convert pass that string into the Deserialize method it wont accept as its expecting a reader. If i change it to
again it causes problems on conversions. Not sure if i've missed something simple? Thanks
All-Star
53011 Points
23596 Posts
Re: Read POST data in request body
Dec 12, 2019 06:28 PM|mgebhard|LINK
The code posted in my original post deserialized an HTTP JSON stream into a C# type.
I tested the code before posting.
It's not clear why you are converting the JSON stream to a string, ReadToEnd(), then deserializing the string. I cannot see your code, type, or JSON so I have no way to reproduce the issue.
Did you, at least, try the code I posted?
Maybe this is what to want?
https://www.newtonsoft.com/json/help/html/DeserializeObject.htm
Member
288 Points
1286 Posts
Re: Read POST data in request body
Dec 12, 2019 07:16 PM|uid250511|LINK
I think what you have posted may do the trick. Let me give it a shot. Thanks