

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Elaborazione di eventi HTTP con Rust
<a name="rust-http-events"></a>

Amazon API Gateway APIs, Application Load Balancers e la [funzione Lambda URLs](urls-configuration.md) possono inviare eventi HTTP a Lambda. Puoi utilizzare la cassa [aws\$1lambda\$1events](https://crates.io/crates/aws_lambda_events) di crates.io per elaborare gli eventi da queste origini.

**Example - Gestione della richiesta proxy di Gateway API**  
Tenere presente quanto segue:  
+ `use aws_lambda_events::apigw::{ApiGatewayProxyRequest, ApiGatewayProxyResponse}`: la cassa [aws\$1lambda\$1events](https://crates.io/crates/aws-lambda-events) include molti eventi Lambda. Per ridurre i tempi di compilazione, utilizza i flag delle funzionalità per attivare gli eventi di cui hai bisogno. Esempio: `aws_lambda_events = { version = "0.8.3", default-features = false, features = ["apigw"] }`.
+ `use http::HeaderMap`: questa importazione richiede l'aggiunta della cassa [http](https://crates.io/crates/http) alle dipendenze.

```
use aws_lambda_events::apigw::{ApiGatewayProxyRequest, ApiGatewayProxyResponse};
use http::HeaderMap;
use lambda_runtime::{service_fn, Error, LambdaEvent};

async fn handler(
    _event: LambdaEvent<ApiGatewayProxyRequest>,
) -> Result<ApiGatewayProxyResponse, Error> {
    let mut headers = HeaderMap::new();
    headers.insert("content-type", "text/html".parse().unwrap());
    let resp = ApiGatewayProxyResponse {
        status_code: 200,
        multi_value_headers: headers.clone(),
        is_base64_encoded: false,
        body: Some("Hello AWS Lambda HTTP request".into()),
        headers,
    };
    Ok(resp)
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    lambda_runtime::run(service_fn(handler)).await
}
```

Il [client di runtime Rust per Lambda](https://github.com/aws/aws-lambda-rust-runtime) fornisce anche un'astrazione su questi tipi di eventi che consente di lavorare con tipi HTTP nativi, indipendentemente dal servizio che invia gli eventi. Il codice seguente è equivalente all'esempio precedente e funziona immediatamente con la funzione Lambda URLs, Application Load Balancers e API Gateway.

**Nota**  
La cassa [lambda\$1http](https://crates.io/crates/lambda_http) utilizza la cassa [lambda\$1runtime](https://crates.io/crates/lambda_runtime) sottostante. Non è necessario eseguire l'importazione di `lambda_runtime` separatamente.

**Example - Gestione delle richieste HTTP**  

```
use lambda_http::{service_fn, Error, IntoResponse, Request, RequestExt, Response};

async fn handler(event: Request) -> Result<impl IntoResponse, Error> {
    let resp = Response::builder()
        .status(200)
        .header("content-type", "text/html")
        .body("Hello AWS Lambda HTTP request")
        .map_err(Box::new)?;
    Ok(resp)
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    lambda_http::run(service_fn(handler)).await
}
```

Per un altro esempio di utilizzo`lambda_http`, consultate l'esempio di [codice http-axum](https://github.com/aws/aws-lambda-rust-runtime/blob/main/examples/http-axum/src/main.rs) nel repository Labs. AWS GitHub 

**Eventi HTTP Lambda di esempio per Rust**
+ [Eventi HTTP Lambda](https://github.com/aws/aws-lambda-rust-runtime/tree/main/examples/http-basic-lambda): una funzione Rust che gestisce gli eventi HTTP.
+ [Eventi HTTP Lambda con intestazioni CORS](https://github.com/aws/aws-lambda-rust-runtime/blob/main/examples/http-cors): una funzione Rust che utilizza Tower per inserire le intestazioni CORS.
+ [Eventi HTTP Lambda con risorse condivise](https://github.com/aws/aws-lambda-rust-runtime/tree/main/examples/basic-shared-resource): una funzione Rust che utilizza risorse condivise inizializzate prima della creazione del gestore della funzione.