Search code examples
c#jsonhttpwebrequest

Make POST() With JSON


I am learning JSON and can not figure out the next step. I have created a JSON Reader and have added the data I need to into my reader, but how do I now actually make a POST() request with the data?

This is what I have

private void PerformJSONPost()
{
    var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://requestb.in/12frf661");
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";

    var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(@"{ ""Security"": { ""UsernameToken"": { ""Username"": ""redrobin"", ""Password"": ""fahidsma!"" } }"), new System.Xml.XmlDictionaryReaderQuotas());

}

EDIT
The issue with this syntax is that the only thing that is posted is:

System.Runtime.Serialization.Json.XmlJsonReader

Solution

  •         var request = WebRequest.CreateHttp(myUrl + "/" + uri);
            request.Credentials = new NetworkCredential(myUserName, myPassword, myDomain);
            request.ContentType = "application/json; charset=utf-8";
            request.Method = "POST";
    
            using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                string jsonstring = JsonConvert.SerializeObject(myJsonobject, Formatting.Indented);
    
                streamWriter.Write(jsonstring);
    
                streamWriter.Flush();
            }
    

    have look above