title: Extract websocket connection Id and other details from websocket in AWS Api Gateway with HTTP integration
body: <p>I have an http endpoint <code>https://websocketsample.free.beeceptor.com</code>
  and an API Gateway in AWS for Websockets with routes for <code>$connect</code>, <code>$disconnect</code> and <code>$default</code>, all of them integrated with HTTP integration to a POST <code>https://websocketsample.free.beeceptor.com</code> so that I can capture the http request.</p>
  <p>When I test it with wscat</p>
  <pre><code>wscat -c wss://e96ypxXXXX.execute-api.eu-west-3.amazonaws.com/production
  </code></pre>
  <p>I can see the HTTP requests received at the HTTP server (in my case beeceptor). The problem is that $connect and $disconnect don't have any payload in the POST request, only http headers. Example:</p>
  <ul>
  <li>When I $connect to websocket I get
  <pre><code>Request body: empty
  Headers:
  {
    &quot;content-length&quot;: &quot;0&quot;,
    &quot;sec-websocket-extensions&quot;: &quot;permessage-deflate; client_max_window_bits&quot;,
    &quot;sec-websocket-key&quot;: &quot;pTR36OHKUSk8cqBix8vJXA==&quot;,
    &quot;sec-websocket-version&quot;: &quot;13&quot;,
    &quot;x-amzn-trace-id&quot;: &quot;Root=1-605322fb-1e2b058363326df80a61df18&quot;,
    &quot;x-forwarded-for&quot;: &quot;185.153.165.121&quot;,
    &quot;x-forwarded-port&quot;: &quot;443&quot;,
    &quot;x-forwarded-proto&quot;: &quot;https&quot;,
    &quot;user-agent&quot;: &quot;AmazonAPIGateway_e96ypxXXXX&quot;,
    &quot;x-amzn-apigateway-api-id&quot;: &quot;e96ypxXXXX&quot;
  }
  </code></pre>
  </li>
  <li>When I send any message to the websocket such as <code>{&quot;action&quot;:&quot;action1&quot;}</code> I get
  <pre><code>Request body: {&quot;action&quot;:&quot;action1&quot;}
  Headers:
  {
    &quot;content-length&quot;: &quot;20&quot;,
    &quot;user-agent&quot;: &quot;AmazonAPIGateway_e96ypxXXXX&quot;,
    &quot;x-amzn-apigateway-api-id&quot;: &quot;e96ypxXXXX&quot;,
    &quot;content-type&quot;: &quot;application/json; charset=UTF-8&quot;
  }
  </code></pre>
  </li>
  <li>And when I $disconnect the websocket I get
  <pre><code>Request body: empty
  Headers:
  {
    &quot;content-length&quot;: &quot;0&quot;,
    &quot;x-api-key&quot;: &quot;&quot;,
    &quot;x-forwarded-for&quot;: &quot;&quot;,
    &quot;x-restapi&quot;: &quot;&quot;,
    &quot;user-agent&quot;: &quot;AmazonAPIGateway_e96ypxXXXX&quot;,
    &quot;x-amzn-apigateway-api-id&quot;: &quot;e96ypxXXXX&quot;
  }
  </code></pre>
  </li>
  </ul>
  <p><strong>How can I get the connection Id so that I can track every websocket session independently?</strong>
  When I tested this with lambda, the request had a requestContext where I could extract the connectionId and use DynamoDb to manage connections. <strong>I don't want to use AWS Lambda</strong>, I have already an http back-end service which can deal with this. But HTTP integration does not seem to provide much information.</p>
  <hr />
  <p><strong>Update 1</strong>: This article may explain some of the reasons <a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-data-mapping.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-data-mapping.html</a>
  Apparently:</p>
  <p>&quot;Data mapping for WebSocket APIs isn't supported in the AWS Management Console. You must use the AWS CLI, AWS CloudFormation, or an SDK to configure data mapping.&quot;</p>
  <p>So it may be that I need to configure data mapping outside the console and hopefully build up a payload that makes sense</p>
  <hr />
  <p><strong>Update 2</strong>: I've added request parameters as per <a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-data-mapping.html#websocket-mapping-request-parameters" rel="nofollow noreferrer">https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-data-mapping.html#websocket-mapping-request-parameters</a> but no luck</p>
  <pre><code>$ aws --profile diegosasw apigatewayv2 update-integration --integration-id 3lppdXX --api-id oe9jal8gXX --request-parameters 'integration.request.header.connectionId'='context.connectionId'
  {
      &quot;ConnectionType&quot;: &quot;INTERNET&quot;,
      &quot;IntegrationId&quot;: &quot;3lppdXX&quot;,
      &quot;IntegrationMethod&quot;: &quot;POST&quot;,
      &quot;IntegrationType&quot;: &quot;HTTP_PROXY&quot;,
      &quot;IntegrationUri&quot;: &quot;https://websocketsample.free.beeceptor.com/connections&quot;,
      &quot;PassthroughBehavior&quot;: &quot;WHEN_NO_MATCH&quot;,
      &quot;PayloadFormatVersion&quot;: &quot;1.0&quot;,
      &quot;RequestParameters&quot;: {
          &quot;integration.request.header.connectionId&quot;: &quot;context.connectionId&quot;
      },
      &quot;TimeoutInMillis&quot;: 29000
  }
  </code></pre>
  <p>A connection request still doesn't get forwarded to beeceptor with connectionId in http headers or anywhere and I don't know how to achieve that.</p>
  <p>Plus.. I found what it seems an issue in API Gateway API Websocket when creating http endpoints: <a href="https://stackoverflow.com/questions/66707833/unable-to-use-different-http-verbs-in-api-gateway-with-api-websocket-and-http-in">Unable to use different HTTP verbs in API Gateway with API Websocket and HTTP integration</a></p>
  <p>Not the most intuitive documentation or usage.</p>
  <hr />
  <p>SOLUTION: As per the marked answer, the solution was as easy as re-deploying the API. The request parameters would be then forwarded properly.</p>
  <pre><code>&quot;Items&quot;: [
          {
              &quot;ConnectionType&quot;: &quot;INTERNET&quot;,
              &quot;IntegrationId&quot;: &quot;okmv0au&quot;,
              &quot;IntegrationMethod&quot;: &quot;POST&quot;,
              &quot;IntegrationType&quot;: &quot;HTTP_PROXY&quot;,
              &quot;IntegrationUri&quot;: &quot;https://awswebsocket.free.beeceptor.com&quot;,
              &quot;PassthroughBehavior&quot;: &quot;WHEN_NO_MATCH&quot;,
              &quot;PayloadFormatVersion&quot;: &quot;1.0&quot;,
              &quot;RequestParameters&quot;: {
                  &quot;integration.request.header.domainName&quot;: &quot;context.domainName&quot;,
                  &quot;integration.request.header.stage&quot;: &quot;context.stage&quot;,
                  &quot;integration.request.header.connectionId&quot;: &quot;context.connectionId&quot;,
                  &quot;integration.request.header.messageId&quot;: &quot;context.messageId&quot;
              },
              &quot;TimeoutInMillis&quot;: 29000
          }
      ]
  </code></pre>