-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
net/http: js-wasm in nodejs HTTP requests fail #69106
Comments
cc @golang/wasm @golang/js @neild |
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
Can you share more information about the environment? |
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. |
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 am running this on MacOS(12.7.6), i followed this tutorial, did u tried with cmd i used? : |
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: |
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:
The text was updated successfully, but these errors were encountered: