Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP Request on wasm return empty #47794

Closed
eduardonunesp opened this issue Aug 18, 2021 · 1 comment
Closed

HTTP Request on wasm return empty #47794

eduardonunesp opened this issue Aug 18, 2021 · 1 comment

Comments

@eduardonunesp
Copy link

eduardonunesp commented Aug 18, 2021

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 Output
$ 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

GOARCH=wasm GOOS=js go build -o static/test-wasm.wasm
// +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("test-wasm.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 don't forget to use 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

@seankhliao
Copy link
Member

This is a CORS issue.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@golang golang locked and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants