foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Assume json content type

Open krzkaczor opened this issue 3 years ago • 5 comments

Component

Anvil

Have you ensured that all of these are up to date?

  • [X] Foundry
  • [X] Foundryup

What version of Foundry are you on?

latest

What command(s) is the bug in?

No response

Operating System

macOS (Apple Silicon)

Describe the bug

Many nodes (hardhat, ganache) will work with ill-formatted RPC requests and will assume that they are dealing with JSON data. For example:

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' http://127.0.0.1:8545

Works with hardhat node, but anvil will throw:

{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid request"}}

My suggestion would be to either:

  1. Improve error message (wrong content-type?)
  2. Assume it's JSON and make it work anyway.

I can work on a fix if you all agree this is a good idea.

krzkaczor avatar Jul 23 '22 19:07 krzkaczor

I have observed hardhat not requiring the content type header, are you sure that geth works without it? I've observed geth not working without Content-Type: application/json. It seems like bad practice to follow lead in introducing bugs, altho it would be nice to have compatibility. cast rpc fixes this

tynes avatar Jul 25 '22 16:07 tynes

Hmmm I am sure that services like alchemy support it.

krzkaczor avatar Jul 28 '22 13:07 krzkaczor

Alchemy most likely uses a proxy in front of Geth, so figuring out Geth's behavior from what Alchemy does is not really possible

onbjerg avatar Jul 28 '22 15:07 onbjerg

Yeah, yeah. What I meant is that it's a wildly spread incorrect behavior.

I just checked that geth/erigon doesn't support it. I'll update the issue description.

krzkaczor avatar Jul 28 '22 16:07 krzkaczor

Note that Infura ignores the client's content-type header entirely. I see how that makes things convenient but I think this is too lax.

usr:~ $ echo -n '[]' | http post http://127.0.0.1:8545
HTTP/1.1 200 OK
access-control-allow-origin: *
content-length: 79
content-type: application/json
date: Fri, 29 Jul 2022 11:30:58 GMT
vary: origin
vary: access-control-request-method
vary: access-control-request-headers

{
    "error": {
        "code": -32600,
        "message": "Invalid request"
    },
    "id": null,
    "jsonrpc": "2.0"
}

maybe a message like

        "message": "The JSON sent is not a valid request object."

sambacha avatar Jul 29 '22 11:07 sambacha

Confirmed that geth does not do this: https://github.com/ethereum/go-ethereum/blob/0be9d76e3702cfae92f042cf6a113e22479de3f5/rpc/http.go#L33-L39

Alchemy and Infura likely just sets it for you

onbjerg avatar Aug 11 '22 18:08 onbjerg

Closing—already done

Evalir avatar Jun 16 '23 14:06 Evalir