

Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 [AWS](https://github.com/awsdocs/aws-doc-sdk-examples)

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# AWS SDKs基本的な例
<a name="apigatewaymanagementapi_code_examples_basics"></a>

次のコード例は、 AWS SDK で Amazon API Gateway Management API を使用する基本的な方法を示しています。

**Contents**
+ [アクション](apigatewaymanagementapi_code_examples_actions.md)
  + [`PostToConnection`](apigatewaymanagementapi_example_apigatewaymanagementapi_PostToConnection_section.md)

# AWS SDKsアクション
<a name="apigatewaymanagementapi_code_examples_actions"></a>

次のコード例は、 AWS SDKs を使用して個々の API Gateway Management API アクションを実行する方法を示しています。それぞれの例には、GitHub へのリンクがあり、そこにはコードの設定と実行に関する説明が記載されています。

 以下の例には、最も一般的に使用されるアクションのみ含まれています。完全なリストについては、「[Amazon API Gateway Management API リファレンス](https://docs.aws.amazon.com/apigateway/latest/api/API_Operations.html)」を参照してください。

**Topics**
+ [`PostToConnection`](apigatewaymanagementapi_example_apigatewaymanagementapi_PostToConnection_section.md)

# AWS SDK または CLI `PostToConnection`で を使用する
<a name="apigatewaymanagementapi_example_apigatewaymanagementapi_PostToConnection_section"></a>

次のサンプルコードは、`PostToConnection` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
**データを WebSocket 接続に送信する方法**  
次の `post-to-connection` の例では、指定した WebSocket API に接続しているクライアントにメッセージを送信します。  

```
aws apigatewaymanagementapi post-to-connection \
    --connection-id L0SM9cOFvHcCIhw= \
    --data "Hello from API Gateway!" \
    --endpoint-url https://aabbccddee.execute-api.us-west-2.amazonaws.com/prod
```
このコマンドでは何も出力されません。  
詳細については、「**Amazon API Gateway ディベロッパーガイド」の「[バックエンドサービスでの @connections コマンドの使用](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-connections.html)」を参照してください。  
+  API の詳細については、AWS CLI コマンドリファレンスの「[PostToConnection](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/apigatewaymanagementapi/post-to-connection.html)」を参照してください。**

------
#### [ Rust ]

**SDK for Rust**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/rustv1/examples/apigatewaymanagement#code-examples)での設定と実行の方法を確認してください。

```
async fn send_data(
    client: &aws_sdk_apigatewaymanagement::Client,
    con_id: &str,
    data: &str,
) -> Result<(), aws_sdk_apigatewaymanagement::Error> {
    client
        .post_to_connection()
        .connection_id(con_id)
        .data(Blob::new(data))
        .send()
        .await?;

    Ok(())
}

    let endpoint_url = format!(
        "https://{api_id}.execute-api.{region}.amazonaws.com/{stage}",
        api_id = api_id,
        region = region,
        stage = stage
    );

    let shared_config = aws_config::from_env().region(region_provider).load().await;
    let api_management_config = config::Builder::from(&shared_config)
        .endpoint_url(endpoint_url)
        .build();
    let client = Client::from_conf(api_management_config);
```
+  API の詳細については、*AWS SDK for Rust API リファレンス*の「[PostToConnection](https://docs.rs/aws-sdk-apigatewaymanagementapi/latest/aws_sdk_apigatewaymanagementapi/client/struct.Client.html#method.post_to_connection)」を参照してください。

------