title: How do you pass Authorization header through API Gateway to HTTP endpoint?
body: <p>I have an API behind an AWS API Gateway that needs to use the Authorization header for processing. I have unfortunately been unable to pass this to the backend for processing.</p>
  
  <p>I have tried creating the Authorization HTTP Request Header in my Method Request and then creating the corresponding Authorization HTTP Header in my Integration Request (Authorization is mapped from method.request.header.Authorization in this case). I log all of the headers that the backend receives, and from the log, I can see other headers that I have listed in the Integration Request but not Authorization. </p>
  
  <p>I have also tried creating a mapping template with Content-Type <code>application/json</code> and the template defined as</p>
  
  <pre><code>  {
        "AccountID": "$context.identity.accountId",
        "Caller": "$context.identity.caller",
        "User": "$context.identity.user",
        "Authorization": "$input.params().header.get('Authorization')",
        "UserARN": "$context.identity.userArn"
    }
  </code></pre>
  
  <p>Yet, the backend logs show that there is still no Authorization header nor any Authorization field in the JSON body. I also cannot see the user's ARN. I have seen other examples and threads where users have mentioned accessing the Authorization field on the event object that is passed into a Lambda function, but I am not using a Lambda function. </p>
  
  <p>I have made sure to Deploy the API Gateway in both scenarios.</p>
  
  <p>Does anyone know if there is some way I can pass the Authorization header through the API Gateway to my HTTP endpoint? Is there an alternative way to access the API caller's user name or ID?</p>
  
  <hr>
  
  <p>Edit - Here's a snippet of the code I'm using to hit the API Gateway:</p>
  
  <pre><code>String awsAccessKey = "myaccesskey";
  String awsSecretKey = "mysecretkey";
  
  URL endpointUrl;
  try {
      endpointUrl = new URL("https://&lt;host&gt;/&lt;path&gt;/&lt;to&gt;/&lt;resource&gt;?startDate=20151201&amp;endDate=20151231");
  } catch(Exception e) {
      throw new RuntimeException("Unable to parse service endpoint: " + e.getMessage());
  }
  
  Date now = new Date();
  
  SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
  sdf1.setTimeZone(new SimpleTimeZone(0, "UTC"));
  String dateTS = sdf1.format(now);
  
  String headerNames = "host;x-amz-date";
  String queryParameters = "endDate=20151231&amp;startDate=20151201";
  
  String canonicalRequest = "GET\n" +
          "/&lt;path&gt;/&lt;to&gt;/&lt;resource&gt;\n" +
          queryParameters + "\n" +
          "host:&lt;host&gt;\n" +
          "x-amz-date:" + dateTS + "\n" +
          "\n" +
          headerNames + "\n" +
          "&lt;sha256 hash for empty request body&gt;";
  
  System.out.println(canonicalRequest);
  
  SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
  sdf2.setTimeZone(new SimpleTimeZone(0, "UTC"));
  String dateStr = sdf2.format(now);
  String scope =  dateStr + "/us-east-1/execute-api/aws4_request";
  String stringToSign =
          "AWS4-HMAC-SHA256\n" +
                 dateTS + "\n" +
                 scope + "\n" +
                 "hex encoded hash of canonicalRequest";
  
  System.out.println(stringToSign);
  
  byte[] kSecret = ("AWS4" + awsSecretKey).getBytes();
  byte[] kDate = HmacSHA256(dateStr, kSecret);
  byte[] kRegion = HmacSHA256("us-east-1", kDate);
  byte[] kService = HmacSHA256("execute-api", kRegion);
  byte[] kSigning = HmacSHA256("aws4_request", kService);
  byte[] signature = HmacSHA256(stringToSign, kSigning);
  String credentialsAuthorizationHeader = "Credential=" + awsAccessKey + "/" + scope;
  String signedHeadersAuthorizationHeader = "SignedHeaders=" + headerNames;
  String signatureAuthorizationHeader = "Signature=" + "hex encoded signature";
  String authorization = "AWS4-HMAC-SHA256 "
          + credentialsAuthorizationHeader + ", "
          + signedHeadersAuthorizationHeader + ", "
          + signatureAuthorizationHeader;
  
  Map&lt;String, String&gt; headers = new HashMap&lt;String, String&gt;();
  headers.put("x-amz-date", dateTS);
  headers.put("Host", endpointUrl.getHost());
  headers.put("Authorization", authorization);
  headers.put("Content-Type", "application/json");
  
  HttpURLConnection connection = null;
  try {
      connection = (HttpURLConnection) endpointUrl.openConnection();
      connection.setRequestMethod("GET");
  
      for (String headerKey : headers.keySet()) {
          connection.setRequestProperty(headerKey, headers.get(headerKey));
      }
      connection.setUseCaches(false);
      connection.setDoInput(true);
      connection.setDoOutput(true);
  
      InputStream is;
      try {
          is = connection.getInputStream();
      } catch (IOException e) {
          is = connection.getErrorStream();
      }
  
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
      StringBuffer response = new StringBuffer();
      while ((line = rd.readLine()) != null) {
          response.append(line);
          response.append('\r');
      }
      rd.close();
      System.out.println(response.toString());
  } catch (Exception e) {
      throw new RuntimeException("Error: " + e.getMessage(), e);
  } finally {
      if (connection != null) {
          connection.disconnect();
      }
  }
  </code></pre>
  
  <p>This is good enough to authenticate successfully and hit the HTTP endpoint on the backend.</p>