Skip to content

Commit 9d7ae45

Browse files
authored
fix(http): include browser headers if not set by user (#1354)
* fix(http): include browser automatic headers if not set by user close #1349 * fmt * Update browser-headers.md * lint * generate api
1 parent 200f61c commit 9d7ae45

4 files changed

Lines changed: 40 additions & 20 deletions

File tree

.changes/browser-headers.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"http-js": "patch"
3+
---
4+
5+
Include headers created by browser if not declared by user, which fixes missing headers like `Content-Type` when using `FormData`.

plugins/http/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/http/guest-js/index.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,30 +117,45 @@ export async function fetch(
117117

118118
const headers = init?.headers
119119
? init.headers instanceof Headers
120-
? Array.from(init.headers.entries())
121-
: Array.isArray(init.headers)
122-
? init.headers
123-
: Object.entries(init.headers)
124-
: [];
125-
126-
const mappedHeaders: Array<[string, string]> = headers.map(([name, val]) => [
127-
name,
128-
// we need to ensure we have all values as strings
129-
// eslint-disable-next-line
130-
typeof val === "string" ? val : (val as any).toString(),
131-
]);
120+
? init.headers
121+
: new Headers(init.headers)
122+
: new Headers();
132123

133124
const req = new Request(input, init);
134125
const buffer = await req.arrayBuffer();
135-
const reqData =
126+
const data =
136127
buffer.byteLength !== 0 ? Array.from(new Uint8Array(buffer)) : null;
137128

129+
// append new headers created by the browser `Request` implementation,
130+
// if not already declared by the caller of this function
131+
for (const [key, value] of req.headers) {
132+
if (!headers.get(key)) {
133+
headers.set(key, value);
134+
}
135+
}
136+
137+
const headersArray =
138+
headers instanceof Headers
139+
? Array.from(headers.entries())
140+
: Array.isArray(headers)
141+
? headers
142+
: Object.entries(headers);
143+
144+
const mappedHeaders: Array<[string, string]> = headersArray.map(
145+
([name, val]) => [
146+
name,
147+
// we need to ensure we have all header values as strings
148+
// eslint-disable-next-line
149+
typeof val === "string" ? val : (val as any).toString(),
150+
],
151+
);
152+
138153
const rid = await invoke<number>("plugin:http|fetch", {
139154
clientConfig: {
140155
method: req.method,
141156
url: req.url,
142157
headers: mappedHeaders,
143-
data: reqData,
158+
data,
144159
maxRedirections,
145160
connectTimeout,
146161
proxy,

plugins/http/src/commands.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct FetchResponse {
4545
rid: ResourceId,
4646
}
4747

48-
#[derive(Deserialize)]
48+
#[derive(Debug, Deserialize)]
4949
#[serde(rename_all = "camelCase")]
5050
pub struct ClientConfig {
5151
method: String,
@@ -57,31 +57,31 @@ pub struct ClientConfig {
5757
proxy: Option<Proxy>,
5858
}
5959

60-
#[derive(Deserialize)]
60+
#[derive(Debug, Deserialize)]
6161
#[serde(rename_all = "camelCase")]
6262
pub struct Proxy {
6363
all: Option<UrlOrConfig>,
6464
http: Option<UrlOrConfig>,
6565
https: Option<UrlOrConfig>,
6666
}
6767

68-
#[derive(Deserialize)]
68+
#[derive(Debug, Deserialize)]
6969
#[serde(rename_all = "camelCase")]
7070
#[serde(untagged)]
7171
pub enum UrlOrConfig {
7272
Url(String),
7373
Config(ProxyConfig),
7474
}
7575

76-
#[derive(Deserialize)]
76+
#[derive(Debug, Deserialize)]
7777
#[serde(rename_all = "camelCase")]
7878
pub struct ProxyConfig {
7979
url: String,
8080
basic_auth: Option<BasicAuth>,
8181
no_proxy: Option<String>,
8282
}
8383

84-
#[derive(Deserialize)]
84+
#[derive(Debug, Deserialize)]
8585
pub struct BasicAuth {
8686
username: String,
8787
password: String,

0 commit comments

Comments
 (0)