How to applying POST method using Python?

Question:

I have struggled with gettting bad response while I request POST method in Python.

API DOCS : https://docs.wemix.com/v/en/dapp-developer/api-reference/account-apis#get-wemix-balance-for-a-single-address

For example, body data is like

data = {
    'addresses': [
        '0xaC45d8b7ED8bDCBaF5F59F4622F9B351CDb9E956',
        '0x5b2d775cA52Ad27524e3581980D56694D9E1A83C',
        '0xc000250d0CdFb43e2F5C03f6ea73807e0694e795',
        '0x501226d260513C4B6f2Ef67B6213AF5A9e3cdC7B',
        '0x779a605C753cea01d23D9BB7a5F961A8AF315011',
        '0x8FEC9850DAd02AB726b6822927c8B402bC993d58',
        '0xeFE753A89B5f1836A44744DF626F5E47eeDa18F7',
        '0xf9F20C6dbaF7D70FCEADDDBD13d9Bcb6b66298DA',
        '0x290Fa3FE65dF509cb27bA55b493841cB92DA083B',
        '0x2ED5972f39109D1818024bF84Ce8F2230c4d99D9',
        '0xbed789c6008f788a28fc222c83082d67033daf7f',
        '0x1393bba2f90fe4d040bf70c24183dd6e27274de5',
        '0xb93de30f956a03d8c597ba0c7a7d08cf4644a571',
        '0xf68aed73c86127de4047533da58b419e1aca7c0a'
    ]
}
    

I tried to get balance of each addresses.

url = 'https://explorerapi.wemix.com/v1/accounts/balance'
headers = {
    'api-key': API_KEY
}
response = requests.get(url, headers=headers, json=data)

But, response is

{'status': '404',
 'message': "Maybe you're requesting not supported http method"}

I could get right response when I use Postman.

How can I fix it correctly?

Asked By: jtoyhh

||

Answers:

You can try:

import json

url = 'https://explorerapi.wemix.com/v1/accounts/balance'
headers = {
    'api-key': '1ba5e446edf1997f67b51bf9e60b3fbba6fa1bf84301115292805d7e24f43539',
    'Content-Type': 'application/json',  # Mandatory, check documentation
}
response = requests.post(url, headers=headers, data=json.dumps(data))

Output:

>>> response.json()

{'status': '200',
 'message': 'success',
 'results': {'data': [{'address': '0xaC45d8b7ED8bDCBaF5F59F4622F9B351CDb9E956',
    'balance': '50000000000000000000'},
   {'address': '0x5b2d775cA52Ad27524e3581980D56694D9E1A83C',
    'balance': '50000000000000000000'},
   {'address': '0xc000250d0CdFb43e2F5C03f6ea73807e0694e795',
    'balance': '50000000000000000000'},
   {'address': '0x501226d260513C4B6f2Ef67B6213AF5A9e3cdC7B',
    'balance': '50000000000000000000'},
   {'address': '0x779a605C753cea01d23D9BB7a5F961A8AF315011',
    'balance': '50000000000000000000'},
   {'address': '0x8FEC9850DAd02AB726b6822927c8B402bC993d58',
    'balance': '50000000000000000000'},
   {'address': '0xeFE753A89B5f1836A44744DF626F5E47eeDa18F7',
    'balance': '50000000000000000000'},
   {'address': '0xf9F20C6dbaF7D70FCEADDDBD13d9Bcb6b66298DA',
    'balance': '50000000000000000000'},
   {'address': '0x290Fa3FE65dF509cb27bA55b493841cB92DA083B', 'balance': '0'},
   {'address': '0x2ED5972f39109D1818024bF84Ce8F2230c4d99D9', 'balance': '0'},
   {'address': '0xBEd789c6008F788a28fc222C83082D67033Daf7F',
    'balance': '60000000000000000000000000'},
   {'address': '0x1393BBA2F90fE4D040Bf70C24183Dd6e27274DE5',
    'balance': '783714822540624807922423'},
   {'address': '0xb93de30f956A03D8c597Ba0c7A7d08Cf4644a571',
    'balance': '499998995799999999958000'},
   {'address': '0xF68AEd73c86127dE4047533Da58b419E1AcA7c0A',
    'balance': '500000089500000000000000'}]}}

Shortcut:

url = 'https://explorerapi.wemix.com/v1/accounts/balance'
headers = {
    'api-key': '1ba5e446edf1997f67b51bf9e60b3fbba6fa1bf84301115292805d7e24f43539',
}
response = requests.post(url, headers=headers, json=data)
Answered By: Corralien
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.