Open sekulicd opened 2 months ago
cc @golang/wasm @golang/js @neild
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
With the following code
//go:build js && wasm
package main
import (
"fmt"
"io"
"log"
"net/http"
"runtime"
"syscall/js"
)
func main() {
fmt.Println(runtime.GOOS)
fmt.Println(runtime.GOARCH)
var p = js.Global().Get("process").Get("version").String()
fmt.Println(p)
c := http.Client{
Transport: http.DefaultTransport,
}
resp, err := c.Get("https://httpbin.org/anything")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
log.Println(string(body))
}
I got
PS T:\gotmp> go run main.go
js
wasm
v22.3.0
2024/08/30 00:34:06 {
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "br, gzip, deflate",
"Accept-Language": "*",
"Host": "httpbin.org",
"Sec-Fetch-Mode": "cors",
"User-Agent": "node",
"X-Amzn-Trace-Id": "Root=1-66d0a2fe-548f0f4a397f75581a5b352b"
},
"json": null,
"method": "GET",
"origin": "20.63.141.121",
"url": "https://httpbin.org/anything"
}
Can you share more information about the environment?
Can you check if this code is working correctly without using the js/wasm target?
Can you check if this code is working correctly without using the js/wasm target?
Yeah, i know its working with go run but not with mentioned node binary, my goal is to make http req from node js using go http pkg
Can you describe the Node.js version you use?
Note that when I use go run, the appropriate wasm wrapper is already present in the environment variable, so I am indeed running a wasm binary, as you can see from the runtime information printed.
Note that when I use go run, the appropriate wasm wrapper is already present in the environment variable, so I am indeed running a wasm binary, as you can see from the runtime information printed.
v21.7.3
PS T:\gotmp> $env:GOARCH="wasm"
PS T:\gotmp> $env:GOOS="js"
PS T:\gotmp> cat .\main.go
//go:build js && wasm
package main
import (
"fmt"
"io"
"log"
"net/http"
"runtime"
"syscall/js"
)
func main() {
fmt.Println(runtime.GOOS)
fmt.Println(runtime.GOARCH)
var p = js.Global().Get("process").Get("version").String()
fmt.Println(p)
c := http.Client{
Transport: http.DefaultTransport,
}
resp, err := c.Get("https://httpbin.org/anything")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
log.Println(string(body))
}
PS T:\gotmp> node --version
v21.7.3
PS T:\gotmp> go version
go version go1.23.0 windows/amd64
PS T:\gotmp> go run main.go
js
wasm
v21.7.3
2024/09/03 22:36:21 {
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "br, gzip, deflate",
"Accept-Language": "*",
"Host": "httpbin.org",
"Sec-Fetch-Mode": "cors",
"User-Agent": "node",
"X-Amzn-Trace-Id": "Root=1-66d71ee2-595ae3d765b1eb967f72c0e7"
},
"json": null,
"method": "GET",
"origin": "8.217.95.142",
"url": "https://httpbin.org/anything"
}
I can't reproduce it with exactly same version on Windows. Can you try to reproduce it on Linux?
I can't reproduce it with exactly same version on Windows. Can you try to reproduce it on Linux?
I am running this on MacOS(12.7.6), i followed this tutorial, did u tried with cmd i used? :
GOOS=js GOARCH=wasm go run -exec="$(go env GOROOT)/misc/wasm/go_js_wasm_exec" .
I can reproduce it on Linux
zxilly@Zxilly-s-PC:/mnt/t/gotmp$ GOOS=js GOARCH=wasm go run -exec="$(go env GOROOT)/misc/wasm/go_js_wasm_exec" .
js
wasm
v22.8.0
2024/09/04 17:39:14 Get "https://httpbin.org/anything": dial tcp: lookup httpbin.org on 127.0.0.53:53: write udp 127.0.0.1:4->127.0.0.53:53: write: Connection reset by peer
exit status 1
I'll dive into it.
Change https://go.dev/cl/611215 mentions this issue: net/http: only disable fetch in test
Go version
go version go1.22.0 darwin/amd64
Output of
go env
in your module/workspace:What did you do?
I want to make HTTP request from Go WASM inside Node JS.
Here is sample main.go
When i try to execute WebAssembly with Node.js with cmd:
I get bellow error:
I understood that with this PR if i add js/wasm build tags that http.DefaultTransport will use RoundTripper with fetch options.
What did you see happen?
What did you expect to see?
Response similar to bellow: