title: Making (POST) signed requests to AWS IAM using C#
body: <p>I am trying follow the example given at AWS documentation <a href="http://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html" rel="nofollow">Signing AWS requests</a> and make a ListUsers call in C#. I have arrived till the last stage of generating the signature (i.e ready to submit the signed request given at <a href="http://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html" rel="nofollow">signature-4 request examples</a>). But the code I pasted below is throwing 'bad request' exception when submitted.</p>
  
  <pre><code> static void submitSignedRequest(string my_access_key, string my_secret_key, string signature, string curr_utc_date_string, string curr_utc_datetime_string)
      {
          string url = "https://iam.amazonaws.com/?Action=ListUsers&amp;Version=2010-05-08&amp;X-Amz-Algorithm=AWS4-HMAC-SHA256&amp;" +
                          "X-Amz-Credential=" + my_access_key + "/" + curr_utc_date_string + "/us-east-1/iam/aws4_request&amp;X-Amz-Date=" + curr_utc_datetime_string +
                              "&amp;X-Amz-SignedHeaders=content-type;host;x-amz-date&amp;X-Amz-Signature=" + signature;
          HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
          request.Method = "POST";
          request.Host = "iam.amazonaws.com";
          request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
          WebHeaderCollection headers = (request as HttpWebRequest).Headers;
  
  
          headers.Add("Authorization", "AWS4-HMAC-SHA256 Credential=" + my_access_key +"/" + curr_utc_date_string + "/us-east-1/iam/aws4_request, " +
              "SignedHeaders=content-type;host;x-amz-date, Signature=" + signature);
          headers.Add("x-amz-date", curr_utc_datetime_string);
  
          HttpWebResponse response;
          try
          {
              response = request.GetResponse() as HttpWebResponse;
          }
          catch (Exception ex)
          {
  
              Console.WriteLine("Error Message:    " + ex.Message);
          }
      }
  </code></pre>
  
  <p>Output i get is:</p>
  
  <pre><code>Error Message:    The remote server returned an error: (400) Bad Request.
  </code></pre>
  
  <p>Can some one help me what wrong I am doing here?</p>
  
  <p>Edit:</p>
  
  <p>I solved this finally by myself. I had to transform it to below. (answer is for RDS though I believe it's visible what the differences are).</p>
  
  <pre><code>string url = "https://rds.us-west-1.amazonaws.com";
          HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  
          //  ASCIIEncoding encoding = new ASCIIEncoding();
          string postData = "Action=DescribeDBInstances&amp;Version=2013-09-09";
          byte[] data = Encoding.UTF8.GetBytes(postData);
  
          request.Method = "POST";
          request.Host = "rds.us-west-1.amazonaws.com";
          request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
          //  request.ContentLength = data.Length;
  
          using (Stream stream = request.GetRequestStream())
          {
              stream.Write(data, 0, data.Length);
          }
  
          WebHeaderCollection headers = (request as HttpWebRequest).Headers;
  
          request.Headers.Add("Authorization", "AWS4-HMAC-SHA256 Credential=" + my_access_key + "/" + curr_utc_date_string + "/us-west-1/rds/aws4_request, " +
              "SignedHeaders=content-type;host;x-amz-date, Signature=" + signature);
          request.Headers.Add("x-amz-date", curr_utc_datetime_string);
  </code></pre>