Skip to content

net/http: no way to set Fetch options when making outgoing http calls with js/wasm #26769

Closed

Description

@eginez

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version devel +c814ac44c0 Thu Jul 19 21:30:27 2018 +0000 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="wasm"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="js"
GOPATH="/Users/eginez/repos/goland"
GOPROXY=""
GORACE=""
GOROOT="/Users/eginez/repos/go"
GOTMPDIR=""
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/pm/tlg5p5nx4vj3s05h4rfsv3t40000gn/T/go-build136489492=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

The native http client, maps to the fetch() api. With the current client it is impossible to set additional options as supported by the fetch call, see: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch.

What did you expect to see?

A way to specify options for the fetch call

Activity

  1. 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
  2. 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
  3. agnivade commented on Aug 2, 2018

    @agnivade
    Contributor

    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.

    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.

  4. bradfitz commented on Aug 2, 2018

    @bradfitz
    Contributor

    @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

  5. bradfitz commented on Aug 2, 2018

    @bradfitz
    Contributor

    (That is, we don't need to wait for a new HTTP client package design to make progress on this)

  6. 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
  7. added this to the Unplanned milestone on Aug 2, 2018
  8. agnivade commented on Aug 2, 2018

    @agnivade
    Contributor

    Ah, using WithContext sounds like a good idea. I was overcomplicating things.

    Also @johanbrandhorst

  9. johanbrandhorst commented on Aug 2, 2018

    @johanbrandhorst
    Member

    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.

  10. bradfitz commented on Aug 2, 2018

    @bradfitz
    Contributor

    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.

  11. agnivade commented on Aug 3, 2018

    @agnivade
    Contributor

    To me, mode and credentials are the 2 most important options that we should expose.

    • mode can take cors, no-cors, same-origin.
    • credentials can take omit, 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 redirect which somehow maps with client.CheckRedirect.

    See here for remaining options - https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters

  12. 7 remaining items

  13. bradfitz commented on Aug 14, 2018

    @bradfitz
    Contributor

    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.

  14. johanbrandhorst commented on Mar 3, 2019

    @johanbrandhorst
    Member

    I intend to submit a PR to add a way to configure the redirect behavior using the same mechanism as the others.

  15. johanbrandhorst commented on Mar 3, 2019

    @johanbrandhorst
    Member

    @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. 
    

    This too might map to some existing API?

    Inputs appreciated.

  16. gopherbot commented on Mar 3, 2019

    @gopherbot
    Contributor

    Change https://golang.org/cl/164666 mentions this issue: net/http: support configuring redirect fetch option

  17. agnivade commented on Mar 4, 2019

    @agnivade
    Contributor

    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.

  18. johanbrandhorst commented on Mar 4, 2019

    @johanbrandhorst
    Member

    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.

  19. JackKCWong commented on Jul 11, 2022

    @JackKCWong

    May I know what's the latest magic headers to set Fetch options? The wiki suggests js.fetch but it seems broken on go1.18 linux/amd64

    $(go env GOROOT)/misc/wasm/go_js_wasm_exec wasm

    net/http: invalid header field name "js.fetch:mode"

  20. neelance commented on Jul 24, 2022

    @neelance
    Member

    @JackKCWong Please check that fetch is available in your JS environment. Note that Node.js does not provide fetch by default.

  21. seankhliao commented on Oct 9, 2025

    @seankhliao
    Member
  22. adrianosela commented on Feb 1, 2026

    @adrianosela

    Hey!

    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)
    }
    

    ...

    http.SetJSFetchCredentials(req, http.JSFetchCredentialsInclude)
    
  23. modified the milestones: Unplanned, Go1.13 on Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions