title: Post json string to ElasticSearch through webrequest c#
body: <p>I want to post json data to elasticsearch through web request. I get bad request for the below code.   Added stack trace logs.     </p>
  
  <pre><code>        string responseFromServer = string.Empty;
          HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:9200/test/test");
          webRequest.Method = "POST";
          string username = "aaa";
          string password = "aaa";
          string auth = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password));
          webRequest.Headers.Add("Authorization", "Basic " + auth);
          webRequest.ContentType = "application/json";
          webRequest.Accept = "application/json";
          using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
          {
              streamWriter.Write(json);
              streamWriter.Flush();
              streamWriter.Close();
          }
          var httpResponse = (HttpWebResponse)webRequest.GetResponse();
          using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
          {
              var result = streamReader.ReadToEnd();
          }
  </code></pre>
  
  <p>Stacktrace:</p>
  
  <pre><code>   at System.Net.HttpWebRequest.GetResponse()
     at APIResponse.Program.posttoelk(String json) in Program.cs:line 85
     at APIResponse.Program.Main(String[] args) in :line 50
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>