The HTTP request on WASM using the Golang is working or partially working, for some reason the request returns but the status code is 0 and the body has 0 bytes, following more details about the test and what is expected.
What version of Go are you using (go version
)?
$ go version
go1.17 darwin/amd64
Does this issue reproduce with the latest release?
Yes, I'm using the latest version of 1.17
What operating system and processor architecture are you using (go env
)?
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/eduardo/Library/Caches/go-build"
GOENV="/Users/eduardo/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/eduardo/.go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/eduardo/.go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/eduardo/.asdf/installs/golang/1.17/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/eduardo/.asdf/installs/golang/1.17/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/eduardo/Projects/test-wasm/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/sp/kd1c03556vx3j0klnmw50tnc0000gn/T/go-build2171896039=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
Compile and run the following code as WASM binary using the following line and try to run on the browser, here it's using Chrome to test on version Version 92.0.4515.131 (Official Build) (x86_64)
# Compile with the following line
GOARCH=wasm GOOS=js go build -o static/wasm-test.wasm
Code for the WASM binary
// +build js,wasm
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://golang.org", nil)
if err != nil {
panic(err)
}
req.Header.Add("js.fetch:mode", "no-cors")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Printf("Code %d Response len %d\n", resp.StatusCode, len(body))
}
Run on server with the following HTML
<html>
<head>
<meta charset="utf-8" />
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("wasm-test.wasm"), go.importObject).then((result) => {
go.run(result.instance);
});
</script>
</head>
<body></body>
</html>
I'm using the following Golang server to test the web server
package main
import (
"flag"
"fmt"
"log"
"net/http"
)
func main() {
port := 8888
directory := "static"
flag.Parse()
http.Handle("/", http.FileServer(http.Dir(directory)))
log.Printf("Serving %s on HTTP port: %d\n", directory, port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
Also using the wasm_exec.js
from the GOROOT libexec/misc/wasm/wasm_exec.js
What did you expect to see?
On browser console expect to see Code 200 Response len 9953
What did you see instead?
Code 0 Response len 0
On Chrome the call stack seems to be ok:
syscall/js.valueCall @ http://localhost:8888/wasm_exec.js:399
$syscall_js.valueCall @ http://localhost:8888/wasm-test.wasm:1
$syscall_js.Value.Call @ http://localhost:8888/wasm-test.wasm:1
$net_http.__Transport_.RoundTrip @ http://localhost:8888/wasm-test.wasm:1
$net_http.send @ http://localhost:8888/wasm-test.wasm:1
$net_http.__Client_.send @ http://localhost:8888/wasm-test.wasm:1
$net_http.__Client_.do @ http://localhost:8888/wasm-test.wasm:1
$main.main @ http://localhost:8888/wasm-test.wasm:1
$runtime.main @ http://localhost:8888/wasm-test.wasm:1
$wasm_pc_f_loop @ http://localhost:8888/wasm-test.wasm:1
$wasm_export_run @ http://localhost:8888/wasm-test.wasm:1
run @ http://localhost:8888/wasm_exec.js:570
(anonymous) @ http://localhost:8888/:9
(anonymous) @ http://localhost:8888/:8