Skip to content

Commit

Permalink
feat(core): add raw headers to HTTP API, closes #2695 (#3053)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jan 7, 2022
1 parent 0a857f8 commit b7a2345
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/api-raw-headers.md
@@ -0,0 +1,5 @@
---
"api": patch
---

Added `rawHeaders` to `http > Response`.
5 changes: 5 additions & 0 deletions .changes/raw-headers.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Add `raw_headers` to `tauri::api::http::ResponseData`.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions core/tauri/src/api/http.rs
Expand Up @@ -351,11 +351,22 @@ impl Response {
let url = self.2;

let mut headers = HashMap::new();
let mut raw_headers = HashMap::new();
for (name, value) in self.1.headers() {
headers.insert(
name.as_str().to_string(),
String::from_utf8(value.as_bytes().to_vec())?,
);
raw_headers.insert(
name.as_str().to_string(),
self
.1
.headers()
.get_all(name)
.into_iter()
.map(|v| String::from_utf8(v.as_bytes().to_vec()).map_err(Into::into))
.collect::<crate::api::Result<Vec<String>>>()?,
);
}
let status = self.1.status().as_u16();

Expand All @@ -377,6 +388,7 @@ impl Response {
url,
status,
headers,
raw_headers,
data,
})
}
Expand All @@ -403,6 +415,8 @@ pub struct ResponseData {
pub status: u16,
/// Response headers.
pub headers: HashMap<String, String>,
/// Response raw headers.
pub raw_headers: HashMap<String, Vec<String>>,
/// Response data.
pub data: Value,
}
4 changes: 4 additions & 0 deletions tooling/api/src/http.ts
Expand Up @@ -128,6 +128,7 @@ interface IResponse<T> {
url: string
status: number
headers: Record<string, string>
rawHeaders: Record<string, string[]>
data: T
}

Expand All @@ -141,6 +142,8 @@ class Response<T> {
ok: boolean
/** The response headers. */
headers: Record<string, string>
/** The response raw headers. */
rawHeaders: Record<string, string[]>
/** The response data. */
data: T

Expand All @@ -150,6 +153,7 @@ class Response<T> {
this.status = response.status
this.ok = this.status >= 200 && this.status < 300
this.headers = response.headers
this.rawHeaders = response.rawHeaders
this.data = response.data
}
}
Expand Down

0 comments on commit b7a2345

Please sign in to comment.