Skip to content

net/http package (net/http: neither of Fetch nor XMLHttpRequest APIs is available) #839

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
pjebs opened this issue Jul 6, 2018 · 5 comments
Labels

Comments

@pjebs
Copy link
Contributor

pjebs commented Jul 6, 2018

I am making an electron app. http.Client works for render process (inside chromium JavaScript) but doesn't work from main process (node js)

It says an error about it not being supported.

How can I make requests from the main process? The compatibility table says I need a polyfill? Why does node js need a polyfill when it works from within chromium. What's the difference?

Is it better to use node library directly or perhaps axios library?

@dmitshur
Copy link
Member

dmitshur commented Jul 6, 2018

http.Client in GopherJS has 2 implementations: one based on Fetch API, another based on XMLHttpRequest API. It picks whichever is available in that order. Both of those tend to be available in browsers.

Node doesn't seem to provide either one by default, that's why you need a polyfill (either for Fetch or for XHR) for http.Client to work under node. The problem seems to be that there wasn't a good one, last time people looked. See previous discussion in #586.

@pjebs
Copy link
Contributor Author

pjebs commented Jul 6, 2018

Thanks. Worked it out using assistance from #586

Step 1:
Add dependency to npm:
npm install xhr2

Step 2:
Create a imports.inc.js file in the same level as your Go files.

Step 3:
Add global.XMLHttpRequest = require('xhr2'); to the imports.inc.js file.

@pjebs pjebs closed this as completed Jul 6, 2018
@pjebs pjebs changed the title Net/http package Net/http package (net/http: neither of Fetch nor XMLHttpRequest APIs is available) Jul 6, 2018
@pjebs pjebs changed the title Net/http package (net/http: neither of Fetch nor XMLHttpRequest APIs is available) net/http package (net/http: neither of Fetch nor XMLHttpRequest APIs is available) Jul 6, 2018
@pjebs
Copy link
Contributor Author

pjebs commented Jul 8, 2018

@shurcooL
It looks like xhr2 does not respect context cancellation despite the documentation stating that it supports abort()

Do you know how I can make cancelling the specific request work?

@pjebs
Copy link
Contributor Author

pjebs commented Aug 4, 2019

What I have learnt:

Dont use net/http to make http requests.
Use xhr go package by honnef which uses XMLHttpRequest behind the scenes.

On node js, you need to still add global.XMLHttpRequest = require('xhr2');

@pjebs pjebs closed this as completed Dec 16, 2019
@pjebs
Copy link
Contributor Author

pjebs commented Dec 16, 2019

I created a package that makes it easy: https://github.com/rocketlaunchr/gopherjs-xhr

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

2 participants