Skip to content

It caused "dial tcp 127.0.0.1:8881: setsockopt: bad address" error when I used client.Do from "net/http" module in golang #663

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

Closed
fearblackcat opened this issue Jul 26, 2017 · 5 comments
Labels

Comments

@fearblackcat
Copy link

Error: Post http://127.0.0.1:8881: dial tcp 127.0.0.1:8881: setsockopt: bad address
    at $callDeferred (/js/testhttp.js:1412:17)
    at $panic (/js/testhttp.js:1451:3)
    at getDataFromPost (/js/testhttp.js:109234:4)
    at Object.v.$externalizeWrapper [as testhttprpc] (/js/testhttp.js:1867:24)
    at Object. (/js/testrpc.js:13:6)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:390:7)
    at startup (bootstrap_node.js:150:9)
    at bootstrap_node.js:505:3
       url := "http://127.0.0.1:8881"
   fmt.Println("URL:>", url)

   var jsonStr = []byte(`{"id": 2}`)
   req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
   req.Header.Set("Content-Type", "application/json")
       client := &http.Client{
   	Transport: &http.Transport{
   		MaxIdleConnsPerHost: 5,
   	},
   }
       resp, err := client.Do(req) // this line caused error
   if err != nil {
   	panic(err)
   }

Does gopherjs support net/http module in golang ?

@fearblackcat fearblackcat changed the title it caused "dial tcp 127.0.0.1:8881: setsockopt: bad address" error when I used client.Do from "net/http" module in golang It caused "dial tcp 127.0.0.1:8881: setsockopt: bad address" error when I used client.Do from "net/http" module in golang Jul 26, 2017
@myitcv
Copy link
Member

myitcv commented Jul 26, 2017

@fearblackcat take a look through the wiki on guidance regarding net/http:

https://github.com/gopherjs/gopherjs/wiki/JavaScript-Tips-and-Gotchas

Line starts "For making HTTP requests..."

@dmitshur
Copy link
Member

Does gopherjs support net/http module in golang ?

Yes, it does. I use net/http in all my frontend programs.

You need to use http.DefaultTransport rather than creating &http.Transport{MaxIdleConnsPerHost: 5}. The default transport is overridden to be a custom transport that uses Fetch API or XHR API.

@fearblackcat
Copy link
Author

fearblackcat commented Jul 27, 2017

@shurcooL I changed the http.Transport to http.DefaultTransport and run my code in nodejs server side. But it also cannot be used.
Error: Post http://127.0.0.1:8881: net/http: neither of Fetch nor XMLHttpRequest APIs is available

It seems that server side doesn't import fetch and xmlhttpRequest package. So is there any way to resolve it?

These is my code.

 client := &http.Client{
		Transport: http.DefaultTransport,
	}
	resp, err := client.Do(req)

Maybe I should try to adopt @myitcv advice

@dmitshur
Copy link
Member

My advice was for using net/http in the browser, which provides XHR and Fetch APIs.

It doesn't work for node, because node doesn't provide those APIs. You'd need to install a polyfill. See #586.

But if you need to run it on backend instead of browser, why not use normal Go compiler?

@fearblackcat
Copy link
Author

@shurcooL Thanks to reply. It helps me enormously

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants