Web3.js 1.0.0-beta.37 call raises {"code":-32600,"message":"invalid json request"}

It was originally raised at https://github.com/ensdomains/ensjs/issues/36

In the issue, it was throwing an error at web3.eth.net.getId but looks like the same error happens in any function call (the test code below is calling getBlockNumber).

const Web3 = require('web3') 
const Web3Legacy = require('web3legacy')
console.log('web3 version', (new Web3()).version)
console.log('web3 legacy version', (new Web3Legacy()).version)
// Do "export INFURA_PROJECT_ID=YOURPROJECTID" on your terminal
const projectid = process.env.INFURA_PROJECT_ID
const endpoint = 'https://mainnet.infura.io/v3/' + projectid
const provider = new Web3.providers.HttpProvider(endpoint)
const web3 = new Web3(provider)
const web3legacy = new Web3Legacy(provider)
async function call(){
    console.log('latest web3')
    console.log('blocknumber', await web3.eth.getBlockNumber())

    console.log('lagacy web3')
    console.log('blocknumber', await web3legacy.eth.getBlockNumber())
}
call()

The above script shows the same error message

web3 version 1.0.0-beta.55
web3 legacy version 1.0.0-beta.37
latest web3
blocknumber 7732404
lagacy web3
(node:57809) UnhandledPromiseRejectionWarning: Error: Node error: {"code":-32600,"message":"invalid json request"}
    at Function.validate (/Users/makoto/work/ens/tmp/ethereum-ens-versions/node_modules/web3-providers/dist/web3-providers.cjs.js:114:18)
    at HttpProvider._callee$ (/Users/makoto/work/ens/tmp/ethereum-ens-versions/node_modules/web3-providers/dist/web3-providers.cjs.js:710:61)
    at tryCatch (/Users/makoto/work/ens/tmp/ethereum-ens-versions/node_modules/regenerator-runtime/runtime.js:45:40)
    at Generator.invoke [as _invoke] (/Users/makoto/work/ens/tmp/ethereum-ens-versions/node_modules/regenerator-runtime/runtime.js:271:22)
    at Generator.prototype.(anonymous function) [as next] (/Users/makoto/work/ens/tmp/ethereum-ens-versions/node_modules/regenerator-runtime/runtime.js:97:21)
    at asyncGeneratorStep (/Users/makoto/work/ens/tmp/ethereum-ens-versions/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/Users/makoto/work/ens/tmp/ethereum-ens-versions/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:57809) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:57809) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Here is the gist of the code and package.json which should be enough to replicate the issue.

It would be great if you can support version 35~37 as they were relatively stable versions and I believe many projects still use the version. We will eventually upgrade web3.js to the latest version but the latest version has lots of breaking changes and need a bit more time to support.

1 Like

Thanks for the detailed report @makoto let me look into it and see what changed in web3js and see what we can do

We ran your test script and logged the RPC request it is making. The one that works from web3 1.0.0-beta.55 is:

INFO:root:POST request,
Path: /
Headers:
Content-Type: application/json
Connection: keep-alive
Host: localhost:8000
User-Agent: Mozilla/5.0 (Darwin x64) node.js/8.15.1 v8/6.2.414.75
Content-Length: 63



Body:
{"jsonrpc":"2.0","id":0,"method":"eth_blockNumber","params":[]}

The one that is failing from 1.0.0-beta.37 is:

INFO:root:POST request,
Path: /
Headers:
Content-Type: application/json
Connection: keep-alive
Host: localhost:8000
User-Agent: Mozilla/5.0 (Darwin x64) node.js/8.15.1 v8/6.2.414.75
Content-Length: 97

Body:
{"jsonrpc":"2.0","id":0,"method":{"jsonrpc":"2.0","id":1,"method":"eth_blockNumber","params":[]}}

As you can see, the legacy version is sending an invalid JSON-RPC request with duplicate json, id, and method items. You might want to check for any dependency conflicts in your packge.json that might be causing this.

Hi, EG. Thank you for the response

I found something interesting. The problem happens if I pass a provider created by beta.55 into the beta.37 Web3 object. If I change to the following, it works.

~/.../tmp/ethereum-ens-versions (master)$node index.js 
web3 version 1.0.0-beta.55
web3 legacy version 1.0.0-beta.37
latest web3
blocknumber 7739166
lagacy web3
blocknumber 7739166
1 Like