title: URL-encoded Query String according to Amazon AWS specs
body: <p>I'm currently implementing an application to generate Amazon AWS Signature Version 4 signatures (for details on the signing process, refer to this page: <a href="http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html" rel="nofollow">http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html</a>).
  Conveniently, Amazon also supplies a test suite for those signatures. There is one test case, however, that I can't really figure out.
  Note that my question only refers to the first step of the signing process (generating the canonical request), and specifically to the creation of the canonical query string.</p>
  
  <p>The test case input HTTP request looks like this:</p>
  
  <pre><code>POST /?@#$%^&amp;+=/,?&gt;&lt;`";:\|][{} =@#$%^&amp;+=/,?&gt;&lt;`";:\|][{}  http/1.1
  Date:Mon, 09 Sep 2011 23:36:00 GMT
  Host:host.foo.com
  </code></pre>
  
  <p>And this is the expected result for the canonical request:</p>
  
  <pre><code>POST
  /
  %20=%2F%2C%3F%3E%3C%60%22%3B%3A%5C%7C%5D%5B%7B%7D&amp;%40%23%24%25%5E=
  date:Mon, 09 Sep 2011 23:36:00 GMT
  host:host.foo.com
  
  date;host
  e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
  </code></pre>
  
  <p>The third line in the canonical request denotes the url-encoded query string. But I don't really get how they get there from the input, even following the rules stated in the Sig V4 documentation:</p>
  
  <p>To construct the canonical query string, complete the following steps:</p>
  
  <p>URI-encode each parameter name and value according to the following rules:</p>
  
  <p>Do not URL-encode any of the unreserved characters that RFC 3986 defines: </p>
  
  <p>A-Z, a-z, 0-9, hyphen ( - ), underscore ( _ ), period ( . ), and tilde ( ~ ).
  Percent-encode all other characters with %XY, where X and Y are hexadecimal characters (0-9 and uppercase A-F).</p>
  
  <p>For example, the space character must be encoded as %20 (not using '+', as some encoding schemes do) and extended UTF-8 characters must be in the form %XY%ZA%BC.</p>
  
  <p>Sort the encoded parameter names by character code (that is, in strict ASCII order). For example, a parameter name that begins with the uppercase letter F (ASCII code 70) precedes a parameter name that begins with a lowercase letter b (ASCII code 98).</p>
  
  <p>Build the canonical query string by starting with the first parameter name in the sorted list.</p>
  
  <p>For each parameter, append the URI-encoded parameter name, followed by the character '=' (ASCII code 61), followed by the URI-encoded parameter value.
  Use an empty string for parameters that have no value.</p>
  
  <p>Append the character '&amp;' (ASCII code 38) after each parameter value except for the last value in the list.</p>
  
  <p>Can someone explain?
  Thanks a bunch in advance!</p>