You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
net/http: no way to set Fetch options when making outgoing http calls with js/wasm #26769
changed the title [-]No way to set options when making outgoing http calls WASM [/-][+]Wasm: No way to set options when making outgoing http calls [/+]on Aug 2, 2018
changed the title [-]Wasm: No way to set options when making outgoing http calls [/-][+]Wasm: No way to set fetch options when making outgoing http calls [/+]on Aug 2, 2018
Yes, this is known. Currently, there is no good way to expose fetch parameters from user level to the http.RoundTripper which is what we are using to make the fetch call. It is not very straightforward, as we have to make some decisions on how best to integrate browser options into a Go API.
The main tracking issue for a better wasm http client is #25695. I would suggest that you comment on that issue with your suggestions on how best we can take this forward.
changed the title [-]Wasm: No way to set fetch options when making outgoing http calls [/-][+]net/http: no way to set Fetch options when making outgoing http calls with js/wasm[/+]on Aug 2, 2018
That's a cool idea, but how do we make it obvious to the user? Is using the context as a transport for arbitrary downstream method invocations something well established? I'd be happy to implement this if there's a proposal.
I don't see an obvious mapping from anything already in net/http to the Fetch redirect knob. So probably we should just do the same thing as the other two.
@agnivade What other options do you think would be reasonable to expose via the header mechanism? I hesitate to close this issue until we cover all the existing options at least. Using your link we have:
Covered by existing API
method
headers
body
mode
credentials
redirect (soon)
signal (covered by context cancellation)
That leaves
cache: The cache mode you want to use for the request.
Not sure if there's some existing API we can map this to
referrer: A USVString specifying no-referrer, client, or a URL. The default is client.
Could presumably be configured via a header like the others.
referrerPolicy: Specifies the value of the referer HTTP header. May be one of no-referrer, no-referrer-when-downgrade, origin, origin-when-cross-origin, unsafe-url.
Could be configured via a header. This seems to affect the Referer header sent, which may already be configurable via existing API (unless that header is ignored when making the request).
integrity: Contains the subresource integrity value of the request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).
This is surely impossible for us to emulate? Or if we can figure out how this is calculated perhaps we could always send it?
keepalive: The keepalive option can be used to allow the request to outlive the page. Fetch with the keepalive flag is a replacement for the Navigator.sendBeacon() API.
I don't think they mean the same concept of keepalive. The Fetch keepalive sounds like an instruction to keep the request alive beyond the lifetime of the client (like a goroutine kind of?). I think HTTP keepalives are used to keep the connection alive. I might be wrong.
Q1: Wondering why headers were chosen over context values, and also why concrete fields for http.Transport e.g. https://go-review.googlesource.com/c/go/+/740960 were not considered as an option?
Q2: if the plan is for these options to remain configurable via these special headers - any reason not to provide functions to set them making them a little more obvious to users e.g.
func SetJSFetchCredentials(r *http.Request, value JSFetchCredentials) {
r.Header.Set(jsFetchCreds, value)
}
Activity
[-]No way to set options when making outgoing http calls WASM [/-][+]Wasm: No way to set options when making outgoing http calls [/+][-]Wasm: No way to set options when making outgoing http calls [/-][+]Wasm: No way to set fetch options when making outgoing http calls [/+]agnivade commentedon Aug 2, 2018
Yes, this is known. Currently, there is no good way to expose fetch parameters from user level to the
http.RoundTripperwhich is what we are using to make the fetch call. It is not very straightforward, as we have to make some decisions on how best to integrate browser options into a Go API.See a very small discussion here - https://go-review.googlesource.com/c/go/+/114515/8/src/net/http/roundtrip_js.go#34
The main tracking issue for a better wasm http client is #25695. I would suggest that you comment on that issue with your suggestions on how best we can take this forward.
bradfitz commentedon Aug 2, 2018
@agnivade, things aren't that doomed. We can use http.Request.WithContext or HTTP headers to something to set options for Fetch.
/cc @neelance @dmitshur
bradfitz commentedon Aug 2, 2018
(That is, we don't need to wait for a new HTTP client package design to make progress on this)
[-]Wasm: No way to set fetch options when making outgoing http calls [/-][+]net/http: no way to set Fetch options when making outgoing http calls with js/wasm[/+]agnivade commentedon Aug 2, 2018
Ah, using
WithContextsounds like a good idea. I was overcomplicating things.Also @johanbrandhorst
johanbrandhorst commentedon Aug 2, 2018
That's a cool idea, but how do we make it obvious to the user? Is using the context as a transport for arbitrary downstream method invocations something well established? I'd be happy to implement this if there's a proposal.
bradfitz commentedon Aug 2, 2018
Using contexts for options is actually generally considered a gross practice. But we can do so if needed.
But before designing anything, what are the specific option(s) we're talking about here? Maybe they already map to something we have in Go.
agnivade commentedon Aug 3, 2018
To me,
modeandcredentialsare the 2 most important options that we should expose.modecan takecors,no-cors,same-origin.credentialscan takeomit,same-origin,include.These are settings which arise only in a browser context. So as I see, there is no clear way to map them in Go.
We also have
redirectwhich somehow maps withclient.CheckRedirect.See here for remaining options - https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters
7 remaining items
bradfitz commentedon Aug 14, 2018
I don't see an obvious mapping from anything already in net/http to the Fetch redirect knob. So probably we should just do the same thing as the other two.
johanbrandhorst commentedon Mar 3, 2019
I intend to submit a PR to add a way to configure the
redirectbehavior using the same mechanism as the others.johanbrandhorst commentedon Mar 3, 2019
@agnivade What other options do you think would be reasonable to expose via the header mechanism? I hesitate to close this issue until we cover all the existing options at least. Using your link we have:
Covered by existing API
That leaves
Not sure if there's some existing API we can map this to
Could presumably be configured via a header like the others.
Could be configured via a header. This seems to affect the
Refererheader sent, which may already be configurable via existing API (unless that header is ignored when making the request).This is surely impossible for us to emulate? Or if we can figure out how this is calculated perhaps we could always send it?
This too might map to some existing API?
Inputs appreciated.
gopherbot commentedon Mar 3, 2019
Change https://golang.org/cl/164666 mentions this issue:
net/http: support configuring redirect fetch optionagnivade commentedon Mar 4, 2019
For keepalive, we have this https://golang.org/pkg/net/http/#Transport.DisableKeepAlives. Is it possible to map to that ?
Other than that, I would just want to add
cache. I have not used the other fields in practice. Will leave it to others to decide.johanbrandhorst commentedon Mar 4, 2019
I don't think they mean the same concept of
keepalive. The Fetchkeepalivesounds like an instruction to keep the request alive beyond the lifetime of the client (like a goroutine kind of?). I think HTTP keepalives are used to keep the connection alive. I might be wrong.net/http: support configuring redirect fetch option
JackKCWong commentedon Jul 11, 2022
May I know what's the latest magic headers to set Fetch options? The wiki suggests
js.fetchbut it seems broken on go1.18 linux/amd64$(go env GOROOT)/misc/wasm/go_js_wasm_exec wasmneelance commentedon Jul 24, 2022
@JackKCWong Please check that
fetchis available in your JS environment. Note that Node.js does not providefetchby default.seankhliao commentedon Oct 9, 2025
I believe this is done, and documented at https://go.dev/wiki/WebAssembly?ref=blog.mozilla.ai#configuring-fetch-options-while-using-nethttp
adrianosela commentedon Feb 1, 2026
Hey!
Q1: Wondering why headers were chosen over context values, and also why concrete fields for
http.Transporte.g. https://go-review.googlesource.com/c/go/+/740960 were not considered as an option?Q2: if the plan is for these options to remain configurable via these special headers - any reason not to provide functions to set them making them a little more obvious to users e.g.
...