From 141668acba7cbbbefcbc4d2fe390e32bd11f1637 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Sun, 3 Feb 2019 11:52:05 +0530 Subject: [PATCH] list all header names in request and response --- src/request.rs | 16 ++++++++++++++++ src/response.rs | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/src/request.rs b/src/request.rs index ddc5ca3..b0a4aad 100644 --- a/src/request.rs +++ b/src/request.rs @@ -201,6 +201,22 @@ impl Request { get_header(&self.headers, name) } + /// A list of the set header names in this request. Lowercased to be uniform. + /// + /// ``` + /// let req = ureq::get("/my_page") + /// .set("X-API-Key", "foobar") + /// .set("Content-Type", "application/json") + /// .build(); + /// assert_eq!(req.header_names(), vec!["x-api-key", "content-type"]); + /// ``` + pub fn header_names(&self) -> Vec { + self.headers + .iter() + .map(|h| h.name().to_ascii_lowercase()) + .collect() + } + /// Tells if the header has been set. /// /// ``` diff --git a/src/response.rs b/src/response.rs index 67594b1..a426e26 100644 --- a/src/response.rs +++ b/src/response.rs @@ -111,6 +111,15 @@ impl Response { .map(|h| h.value()) } + /// A list of the header names in this response. + /// Lowercased to be uniform. + pub fn headers_names(&self) -> Vec { + self.headers + .iter() + .map(|h| h.name().to_ascii_lowercase()) + .collect() + } + /// Tells if the response has the named header. pub fn has<'a>(&self, name: &'a str) -> bool { self.header(name).is_some()