title: 500: internal server error while trying to call a REST api from C# console
body: <p>I m trying to generate a token from salesforce api with oauth. I m using this token to retreive data and then perform put request for each object. However, the web response is showing an unhandled exception saying "500:Internal server error" while i m trying to generate a token. I need to pass content type and accept in header params and other params in body.</p>
  
  <pre><code>namespace Test
  {
      class Program
      {
          static void Main(string[] args)
          {
  
              string response = Post("https://xx-dev.cs15.my.salesforce.com/services/oauth2/token");
              dynamic data = JObject.Parse(response);
              string accessToken = data.access_token;
  
  
          }
          //POST to generate token.
          private static string Post(string url)
          {
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
              request.Method = "POST";
              request.Accept = "application/json";
              request.ContentType = "application/x-www-form-urlencoded";
              string body = "client_id=43nmbmn43534y98jh_3yQty7vm3CqYMvrSdfdsfFDSGFW7kNYk_bWxmhTaY5KT&amp;client_secret=123456789&amp;grant_type=password&amp;username=user@username.com.test&amp;password=TestPassword";
              byte[] byteArray = Encoding.UTF8.GetBytes(body);
              request.ContentLength = byteArray.Length;
  
              Stream dataStream = request.GetRequestStream();
              dataStream.Write(byteArray, 0, byteArray.Length);
              dataStream.Close();
              WebResponse response = request.GetResponse();//**I'm getting the error here**
              dataStream = response.GetResponseStream();
              StreamReader reader = new StreamReader(dataStream);
              string responseFromServer = reader.ReadToEnd();
              reader.Close();
              dataStream.Close();
              response.Close();
  
              return responseFromServer;
          }
  </code></pre>
  
  <p>Headers:
  <a href="https://i.stack.imgur.com/PO6zD.png" rel="nofollow noreferrer">Postman</a>;
  <a href="https://i.stack.imgur.com/Fhf7c.png" rel="nofollow noreferrer">Application</a></p>