Skip to content

Commit 2f1f7e4

Browse files
authored
forward RequestContext in a http header (#108)
1 parent e221708 commit 2f1f7e4

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ exclude = ["examples"]
1919
[dependencies]
2020
http = "0.2.4"
2121
lambda-extension = "0.7.0"
22-
lambda_http = "0.7.1"
22+
lambda_http = "0.7.2"
2323
reqwest = { version = "0.11", default-features = false, features = ["json"] }
2424
tokio = { version = "1.20.0", features = ["macros", "io-util", "sync", "rt-multi-thread", "time"] }
2525
tokio-retry = "0.3"

src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ use std::{
1313
time::Duration,
1414
};
1515

16+
use http::header::{HeaderName, HeaderValue};
1617
use http_body::Body as HttpBody;
1718
use lambda_extension::Extension;
19+
use lambda_http::aws_lambda_events::serde_json;
1820
pub use lambda_http::Error;
1921
use lambda_http::{Body, Request, RequestExt, Response};
2022
use reqwest::{redirect, Client, Url};
@@ -220,6 +222,7 @@ async fn fetch_response(
220222
ready_at_init.store(true, Ordering::SeqCst);
221223
}
222224

225+
let request_context = event.request_context();
223226
let path = event.raw_http_path();
224227
let mut path = path.as_str();
225228
let (parts, body) = event.into_parts();
@@ -229,7 +232,13 @@ async fn fetch_response(
229232
path = path.trim_start_matches(base_path);
230233
}
231234

232-
let req_headers = parts.headers;
235+
let mut req_headers = parts.headers;
236+
237+
// include request context in http header "x-amzn-request-context"
238+
req_headers.append(
239+
HeaderName::from_static("x-amzn-request-context"),
240+
HeaderValue::from_bytes(serde_json::to_string(&request_context).unwrap().as_bytes()).unwrap(),
241+
);
233242

234243
let mut app_url = domain;
235244
app_url.set_path(path);

0 commit comments

Comments
 (0)