Answered by:
Read POST data in request body

Question
-
User-284642143 posted
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?
Thursday, December 12, 2019 1:16 PM
Answers
-
User475983607 posted
Read the input stream. Assuming Web Forms...
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(); } } }
https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 12, 2019 2:39 PM -
User475983607 posted
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(); }
https://www.newtonsoft.com/json/help/html/DeserializeObject.htm
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 12, 2019 6:28 PM
All replies
-
User475983607 posted
Read the input stream. Assuming Web Forms...
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(); } } }
https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 12, 2019 2:39 PM -
User-284642143 posted
Thanks, thats almost got me back on track.
If i have
var rte = new StreamReader(context.Request.InputStream).ReadToEnd();
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
StreamReader sr = new StreamReader(stream).ReadToEnd();
again it causes problems on conversions. Not sure if i've missed something simple? Thanks
Thursday, December 12, 2019 5:08 PM -
User475983607 posted
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(); }
https://www.newtonsoft.com/json/help/html/DeserializeObject.htm
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 12, 2019 6:28 PM -
User-284642143 posted
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. ThanksThursday, December 12, 2019 7:16 PM