why is Python requests not working as intended
Question:
I want to get some info from an JSON RPC API endpoint.
the equivalent curl command is this:
curl 'https://api.mainnet-beta.solana.com' -X POST -H "Content-Type: application/json" -d '
{
"jsonrpc": "2.0", "id": 1,
"method": "getTokenAccountBalance",
"params": [
"FYj69uxq52dee8AyZbRyNgkGbhJdrPT6eqMhosJwaTXB"
]
}
'
it works and response is:
{"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.13.5","slot":176510490},"value":{"amount":"68662508944","decimals":5,"uiAmount":686625.08944,"uiAmountString":"686625.08944"}},"id":1}
but when I want to make a call in my python script:
headers={'Content-type': 'application/json'}
data1 = {
"jsonrpc": "2.0",
"id": 1,
"method": "getTokenAccountBalance",
"params": [
"FYj69uxq52dee8AyZbRyNgkGbhJdrPT6eqMhosJwaTXB",
]
}
response = requests.post('https://api.mainnet-beta.solana.com',headers=headers,data=data1)
print(response.text)
I get this:
{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}
what could be the issue?
Answers:
That’s because you should be using json
argument of the requests.post
function, not the data
argument.
This is how you fix it:
import requests
headers = {'Content-type': 'application/json'}
data1 = {
"jsonrpc": "2.0",
"id": 1,
"method": "getTokenAccountBalance",
"params": [
"FYj69uxq52dee8AyZbRyNgkGbhJdrPT6eqMhosJwaTXB",
]
}
response = requests.post('https://api.mainnet-beta.solana.com',
headers=headers,
json=data1)
print(response.text)
Notice the json
argument of the requests.post
.
I want to get some info from an JSON RPC API endpoint.
the equivalent curl command is this:
curl 'https://api.mainnet-beta.solana.com' -X POST -H "Content-Type: application/json" -d '
{
"jsonrpc": "2.0", "id": 1,
"method": "getTokenAccountBalance",
"params": [
"FYj69uxq52dee8AyZbRyNgkGbhJdrPT6eqMhosJwaTXB"
]
}
'
it works and response is:
{"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.13.5","slot":176510490},"value":{"amount":"68662508944","decimals":5,"uiAmount":686625.08944,"uiAmountString":"686625.08944"}},"id":1}
but when I want to make a call in my python script:
headers={'Content-type': 'application/json'}
data1 = {
"jsonrpc": "2.0",
"id": 1,
"method": "getTokenAccountBalance",
"params": [
"FYj69uxq52dee8AyZbRyNgkGbhJdrPT6eqMhosJwaTXB",
]
}
response = requests.post('https://api.mainnet-beta.solana.com',headers=headers,data=data1)
print(response.text)
I get this:
{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}
what could be the issue?
That’s because you should be using json
argument of the requests.post
function, not the data
argument.
This is how you fix it:
import requests
headers = {'Content-type': 'application/json'}
data1 = {
"jsonrpc": "2.0",
"id": 1,
"method": "getTokenAccountBalance",
"params": [
"FYj69uxq52dee8AyZbRyNgkGbhJdrPT6eqMhosJwaTXB",
]
}
response = requests.post('https://api.mainnet-beta.solana.com',
headers=headers,
json=data1)
print(response.text)
Notice the json
argument of the requests.post
.