1

I receive the following error when trying to connect to my private ethereum node with React Native.

[Unhandled promise rejection: Error: Invalid JSON RPC response: "<html>\r\n<head><title>401 Authorization Required</title></head>\r\n<body>\r\n<center><h1>401 Authorization Required</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n"]

Here are my codes:

global.Web3 = require("web3");
global.Tx = require("ethereumjs-tx");
global.web3 = new Web3(
  new Web3.providers.HttpProvider('https://<username>:<password>@<ethereum node url>'),
);

I noted that this happens only when I attempt to connect to a node that requires me to use the pattern 'username:password@nodeurl' because connecting to Infura has no problem at all.

Also, this problem occurs only on React Native. Running this code on Node.JS works fine.

My web3 version is 1.0.0-beta.34.

0

Managed to solve this with some hacks to HttpProvider.

The solution involves making some some changes to \node_modules\web3-providers-http\src\index.js.

I passed the authorization to HttpProvider as a header like this.

new Web3(new Web3.providers.HttpProvider('https://node-url',0,{Authorization: 'Basic myauthenticationstring'}))

And then at \node_modules\web3-providers-http\src\index.js., i changed the following

HttpProvider.prototype._prepareRequest = function(){ var request = new XHR2();

request.open('POST', this.host, true);
request.setRequestHeader('Content-Type','application/json');

if(this.headers) {
                            request.setRequestHeader(Object.keys(this.headers)[0], Object.values(this.headers)[0]);
                            /*
    this.headers.forEach(function(header) {
                                            console.log(Object.keys(header.headers));
                                            console.log(Object.values(header.headers));
        //request.setRequestHeader(header.name, header.value);
    });
                            */
}

return request;

};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.