Skip to content

Instantly share code, notes, and snippets.

@birksy89
Created March 1, 2017 10:29
Show Gist options
  • Save birksy89/95daa17fa4ce67313d7b43ca4ceb1b04 to your computer and use it in GitHub Desktop.
Save birksy89/95daa17fa4ce67313d7b43ca4ceb1b04 to your computer and use it in GitHub Desktop.
Post JSON Data From Serverside
using Newtonsoft.Json;
using System.Net;
using System.IO;
public void postJSON()
{
postData pd = new postData();
pd.Name = "Steve";
pd.Age = 54;
pd.Sex = "M";
string jsonObj = JsonConvert.SerializeObject(pd);
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://rest.learncode.academy/api/birksy89/a");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(jsonObj);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
public class postData
{
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment