Description
Go version
go version go1.23.5 darwin/arm64
Output of go env
in your module/workspace:
GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/andrea/Library/Caches/go-build'
GOENV='/Users/andrea/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/andrea/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/andrea/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.23.5/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.23.5/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.5'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/andrea/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/sq/sd718h_s6lq1hgfdnt_qq3d00000gn/T/go-build2937344254=/tmp/go-build -gno-record-gcc-switches -fno-common'
What did you do?
I'm trying to get the url i've been redirected to.
package main
import (
"log"
"net/http"
)
func main() {
resp, err := http.Get("https://mock.httpstatus.io/301")
if err != nil {
log.Fatalln(err)
}
log.Printf("%s", resp.Request.URL.String())
}
build with GOARCH=wasm GOOS=js go build -o test.wasm
Repro: https://github.com/andreademasi/go-redirect-repro
What did you see happen?
resp.Request.URL.String()
is equal to "https://mock.httpstatus.io/301"
What did you expect to see?
resp.Request.URL.String()
is equal to "https://mock.httpstatus.io/200"
I understand that this happens because the browser automatically follows redirects and it is not possible to alter this behaviour under normal circumstances.
I also tried using http.Client.CheckRedirect but the function never got called while running in wasm environment.
The fetch API does expose the redirected
property on the Response
object to detect if a request got redirected or not (https://developer.mozilla.org/en-US/docs/Web/API/Response/redirected) and the url
property contains the url you're redirected to.
As a workaround this issue, I patched my own version of go to reflect the behaviour i'd like to see:
// net/http/response.go
type Response struct {
...
// Redirected indicates whether or not the response is the result of a redirect
Redirected bool
}
// net/http/roundtrip_js.go
...
redirected := result.Get("redirected").Bool()
if redirected {
if url, err := url.ParseRequestURI(result.Get("url").String()); err != nil {
errCh <- fmt.Errorf("net/http: failed to parse response url: %v", err)
return nil
} else {
req.URL = url
}
}
respCh <- &Response{
Status: fmt.Sprintf("%d %s", code, StatusText(code)),
StatusCode: code,
Header: header,
ContentLength: contentLength,
Uncompressed: uncompressed,
Body: body,
Request: req,
Redirected: redirected,
}
...
I'll be willing to open a PR to patch this issue (if needed) and/or discuss a different approach