pawelad / pymonzo

Modern Python API client for Monzo public API.
https://pymonzo.pawelad.dev/
Mozilla Public License 2.0
21 stars 20 forks source link

Error during withdrawal: must be greater than 0 (bad_request.bad_param.bad_param.amount) #41

Closed billettg closed 1 week ago

billettg commented 2 months ago

Hi, when attempting a pot withdrawal I'm seeing the below error.

I see the same issue when using 'Monzo Playground' with the same formatted URL.

Any idea how I resolve this?

           DEBUG    Checking pot pot_0000AO5qqpxk15iNCtuwPR with balance 4000                                                                  monzo.py:159
           DEBUG    Withdrawing 100 from pot_id: pot_0000AO5qqpxk15iNCtuwPR to account_id: acc_00009pesbz1XQPrGnIBZYI                           monzo.py:94
           DEBUG    Request details: pot_id=pot_0000AO5qqpxk15iNCtuwPR, account_id=acc_00009pesbz1XQPrGnIBZYI, amount=100                      monzo.py:100
           DEBUG    send_request_headers.started request=<Request [b'PUT']>                                                                    _trace.py:45
           DEBUG    send_request_headers.complete                                                                                              _trace.py:45
           DEBUG    send_request_body.started request=<Request [b'PUT']>                                                                       _trace.py:45
           DEBUG    send_request_body.complete                                                                                                 _trace.py:45
           DEBUG    receive_response_headers.started request=<Request [b'PUT']>                                                                _trace.py:45
           DEBUG    receive_response_headers.complete return_value=(b'HTTP/1.1', 400, b'Bad Request', [(b'Date', b'Thu, 04 Jul 2024 08:48:47   _trace.py:45
                    GMT'), (b'Content-Type', b'application/json'), (b'Content-Length', b'286'), (b'Connection', b'keep-alive'),                            
                    (b'opentracing-id', b'2d070408f38e96c8f2c7d037fd28df51'), (b'strict-transport-security', b'max-age=31536000;                           
                    includeSubDomains; preload'), (b'terror', b'1'), (b'trace-id', b'bcbb30ba-1c2e-49e6-417a-34e86458a46f'),                               
                    (b'CF-Cache-Status', b'DYNAMIC'), (b'X-Content-Type-Options', b'nosniff'), (b'Server', b'cloudflare'), (b'CF-RAY',                     
                    b'89dddbbace3ebeda-LHR')])                                                                                                             
           INFO     HTTP Request: PUT                                                                                                       _client.py:1026
                    https://api.monzo.com/pots/pot_0000AO5qqpxk15iNCtuwPR/withdraw?destination_account_id=acc_00009pesbz1XQPrGnIBZYI&amount                
                    =100&dedupe_id=XSUDq8c6up2nFo0zS45Ugg "HTTP/1.1 400 Bad Request"                                                                       
           DEBUG    receive_response_body.started request=<Request [b'PUT']>                                                                   _trace.py:45
           DEBUG    receive_response_body.complete                                                                                             _trace.py:45
           DEBUG    response_closed.started                                                                                                    _trace.py:45
           DEBUG    response_closed.complete                                                                                                   _trace.py:45
           ERROR    Error during withdrawal: must be greater than 0 (bad_request.bad_param.bad_param.amount)   
{
  "code": "bad_request.bad_param.bad_param.amount",
  "message": "must be greater than 0",
  "params": {
    "amount": "0",
    "dedupe_id": "",
    "destination_account_id": "",
    "source_pot_id": "pot_0000AO5qqpxk15iNCtuwPR",
    "user_id": "user_00009pemh8jk8lJUVaZKIE"
  },
def withdraw_from_pot(monzo, pot_id, account_id, amount, debug):
    logging.debug(f"Withdrawing {amount} from pot_id: {pot_id} to account_id: {account_id}")

    # Log the request details
    logging.debug(f"Request details: pot_id={pot_id}, account_id={account_id}, amount={amount}")

    try:
        result = monzo.pots.withdraw(amount=amount, pot_id=pot_id, account_id=account_id)
        if debug:
            logging.debug(f"Withdraw result: {json.dumps(result, indent=4, cls=DateTimeEncoder, ensure_ascii=False)}")
        logging.info(f"Withdrew {amount} from pot {pot_id} to account {account_id}")
        return result
    except Exception as e:
        logging.error(f"Error during withdrawal: {e}")
        raise
pawelad commented 1 week ago

Sorry for getting back to you so late and thank you for reporting.

It was actually just fixed it in #39 - as found out by @m-roberts, it turns out that certain Monzo API endpoints (including pot withdrawal and deposit) require you send the request data via 'form data' and not 'query params'.

pawelad commented 1 week ago

This should be fixed in the (just released) v2.2.0

billettg commented 1 week ago

Brilliant, thank you.