Apply extra HTTP headers to a page session via Network.setExtraHTTPHeaders.
(
client: &mut CdpClient,
session_id: &str,
extra_headers: Option<&str>,
)
| 10 | |
| 11 | /// Apply extra HTTP headers to a page session via Network.setExtraHTTPHeaders. |
| 12 | pub async fn apply_extra_headers( |
| 13 | client: &mut CdpClient, |
| 14 | session_id: &str, |
| 15 | extra_headers: Option<&str>, |
| 16 | ) -> Result<()> { |
| 17 | if let Some(headers_json) = extra_headers { |
| 18 | let headers: serde_json::Value = serde_json::from_str(headers_json) |
| 19 | .map_err(|e| anyhow::anyhow!("Invalid --extra-headers JSON: {e}"))?; |
| 20 | let headers_obj = headers |
| 21 | .as_object() |
| 22 | .ok_or_else(|| anyhow::anyhow!("--extra-headers must be a JSON object"))?; |
| 23 | let mut converted_headers = serde_json::Map::new(); |
| 24 | for (k, v) in headers_obj { |
| 25 | let val_str = match v { |
| 26 | serde_json::Value::String(s) => s.clone(), |
| 27 | serde_json::Value::Number(n) => n.to_string(), |
| 28 | serde_json::Value::Bool(b) => b.to_string(), |
| 29 | _ => anyhow::bail!( |
| 30 | "Header value for '{}' must be a primitive scalar (string, number, or boolean)", |
| 31 | k |
| 32 | ), |
| 33 | }; |
| 34 | converted_headers.insert(k.clone(), serde_json::Value::String(val_str)); |
| 35 | } |
| 36 | client |
| 37 | .send_to_target(session_id, "Network.enable", json!({})) |
| 38 | .await?; |
| 39 | client |
| 40 | .send_to_target( |
| 41 | session_id, |
| 42 | "Network.setExtraHTTPHeaders", |
| 43 | json!({"headers": converted_headers}), |
| 44 | ) |
| 45 | .await?; |
| 46 | } |
| 47 | Ok(()) |
| 48 | } |
| 49 | |
| 50 | /// Clear extra HTTP headers for a page session. |
| 51 | pub async fn clear_extra_headers(client: &mut CdpClient, session_id: &str) -> Result<()> { |
no test coverage detected