title: HttpWebResponse: The remote server returned an error: (400) Bad Request
body: <p>I am trying to post code my gist account. It works if I write a single line of code. But if I write multiline code, I get the error.</p>
  
  <p>For example for running <strong>[1]</strong>:</p>
  
  <pre><code>Console.WriteLine();
  </code></pre>
  
  <p>For example for not working <strong>[2]</strong>:</p>
  
  <pre><code>python main.py
  
  &gt;&gt;&gt; Programming Quotes
  
  UNIX was not designed to stop its users from doing stupid things, as that would
  also stop them from doing clever things. - Doug Gwyn
  
  python main.py
  
  &gt;&gt;&gt; Programming Quotes
  
  And folks, let's be honest. Sturgeon was an optimist. Way more than 90% of code
  is crap. - viro
  </code></pre>
  
  <p>I can't post the code in the second example. I get "<strong>The remote server returned an error: (400) Bad Request.</strong>" error.</p>
  
  <p><strong>Maybe You Need To Know</strong></p>
  
  <p>textBox1.Text for gist description</p>
  
  <p>richTextBox1.Text for content body --> not working for multiline post datas</p>
  
  <p><strong>My code below:</strong></p>
  
  <pre><code>String jsonMessage = "{ \"description\": \"" +textBox1.Text + "\",  \"public\": true,"
                     + "\"files\": {   \"file1.cs\": {"
                     + "\"content\":\""+ richTextBox1.Text.ToString() + "\"  } }}";
  
              String _url = "https://api.github.com/gists";
  
              HttpWebRequest req = WebRequest.Create(new Uri(_url)) as HttpWebRequest;
  
  
              String userName = "GITHUB_USERNAME";
              String userPassword = "GITHUB_PASSWORD";
              string authInfo = userName + ":" + userPassword;
              authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
  
              req.Method = "POST";
              req.ContentType = "application/json";
              req.Headers.Add("Authorization", "Basic " + authInfo);
              req.UserAgent = "Test-Agent";
              StreamWriter writer = new StreamWriter(req.GetRequestStream());
  
              //MessageBox.Show(jsonMessage);
              writer.Write(jsonMessage);
              writer.Close();
  
              string result = null;
  
              using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse) // Error Line
              {
                  StreamReader reader = new StreamReader(resp.GetResponseStream());
                  result = reader.ReadToEnd();
                  var jo = JObject.Parse(result);
                  //MessageBox.Show(jo["url"].ToString());
                  textBox2.Text = "https://gist.github.com/" + jo["id"].ToString();
  
              }
  </code></pre>
  
  <p>How Can I solve this problem? What should I do?</p>
  
  <p>Thanks!</p>