Posting Json Data to a RESTful API
A useful block of code for serializing a json string to post to a RESTful API. This example doesn't include authentication.
using System.Web.Script.Serialization;
System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http:\\mywebsite.cloudapp.net\api\MyPacket");
request.Method = "POST";
request.ContentType = "application/json";
JavaScriptSerializer serializer = new JavaScriptSerializer();
using (var sw = new StreamWriter(request.GetRequestStream()))
{
string json = serializer.Serialize(requestParamObject);
sw.Write(json);
sw.Flush();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();