Home > front end >  POST requests with additional parameters in Python
POST requests with additional parameters in Python

Time:10-06

I would like to translate this POST request into Python:

{"jsonrpc":"2.0","id":7,"method":"eth_call","params":[{"from":"0x0000000000000000000000000000000000000000","data":"0x41108cf20000000000000000000000000000000000000000000000000000000000000032","to":"0xf86048dff23cf130107dfb4e6386f574231a5c65"},"latest"]}

but I do not understand what I should do with the "params", as if I just copy paste the requests it fails :

import requests
r = requests.post('https://mainnet.optimism.io/', {"jsonrpc":"2.0","id":7,"method":"eth_call","params":[{"from":"0x0000000000000000000000000000000000000000","data":"0x41108cf20000000000000000000000000000000000000000000000000000000000000032","to":"0xf86048dff23cf130107dfb4e6386f574231a5c65"},"latest"]})

r.json()

CodePudding user response:

Try:

import requests

payload = {
    "jsonrpc": "2.0",
    "id": 7,
    "method": "eth_call",
    "params": [
        {
            "from": "0x0000000000000000000000000000000000000000",
            "data": "0x41108cf20000000000000000000000000000000000000000000000000000000000000032",
            "to": "0xf86048dff23cf130107dfb4e6386f574231a5c65",
        },
        "latest",
    ],
}

r = requests.post("https://mainnet.optimism.io/", json=payload)
print(r.json())

Prints:

{'jsonrpc': '2.0', 'result': '0x00000000000000000000000000000000000000000000000000555a5d1d54df34', 'id': 7}
  • Related