I've done a POST request with the HTTPClient class but now I'm struggling a bit. How can I achieve displaying it on my website? The goal is to post a json ( currently in a string) and then display it on the site again. As you can see my POST command goes to the home controller in mvc and then the about action. How can I now get the data and return it to a view?
public ActionResult About(string json)
Does adding paramters like this work? And if so how do I do it properly?
This is the method call:
post(JsonConvert.SerializeXmlNode(pack));
This is the method itself:
async Task post(string jsonText)
{
// Create a New HttpClient object and dispose it when done, so the app doesn't leak resources
using (HttpClient client = new HttpClient())
{
// Call asynchronous network methods in a try/catch block to handle exceptions
try
{
StringContent json = new StringContent(jsonText, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("http://localhost:60000/home/about", json);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);
Console.WriteLine(responseBody);
}catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
}
Update
How can i give the PostAsync command my own class? It forces me give it a HttpContent.
As suggested i have created a new method to cache the whole data but what parameter do i have to give the method which saves the data to receive the class or httpcontent? (in the controller)
[HttpPost]
public HttpResponseMessage Data(//What parameter?)
{
//SaveData
return new HttpResponseMessage(HttpStatusCode.OK);
}