Forum Discussion

ashk's avatar
ashk
Icon for Cirrus rankCirrus
Feb 12, 2024
Solved

iControl REST API Pool Member disable

Hello Everyone, 

 

Iam working on an script to disable the pool members but seems we are getting issues, I tried multiple types but still its not working. Could someone review the below and quote me if Iam missing anything? 

 

From Curl:   

>curl -sku admin:pass -H "Content-Type: application/json" -X PATCH https://f5lab1.com/mgmt/tm/ltm/pool/pool_1/members/~Common~10.170.xx.xx:21 -d '{"session":"user-disabled", "state":"user-down"}'

 

Error:  {"code":400,"message":"Found invalid JSON body in the request.","errorStack":[],"apiError":1}

 

# Function to disable a member in the pool ( Function using Py)

def disable_member(pool_name, member_name):

    url = f'https://{F5_HOST}/mgmt/tm/ltm/pool/{pool_name}/members/~Common~{member_name}'

    auth = (F5_USER, F5_PASSWORD)

    data = { "session": "user-disabled" }

    try:

        response = requests.patch(url, auth=auth, json=data, verify=False)

        return response.status_code == 200

    except Exception as e:

        print(f"Error disabling member: {e}")

        return False

 

Error: 400 BAD REQUEST

  • also FYI...if you get a working postman sample, you can click the </> icon and find samples in a lot of tools/languages of that specific query.

     

    And the python sample they shared from my working postman PATCH is:

    import requests
    import json
    
    url = "https://ltm15/mgmt/tm/ltm/pool/~Common~nginx-pool/members/~Common~172.16.102.5:80"
    
    payload = json.dumps({
      "session": "user-disabled",
      "state": "user-down"
    })
    headers = {
      'Content-Type': 'application/json',
      'Authorization': 'Basic notsofastmyfriends',
      'Cookie': 'BIGIPAuthCookie=supersecretpassphrasehere; BIGIPAuthUsernameCookie=admin'
    }
    
    response = requests.request("PATCH", url, headers=headers, data=payload)
    
    print(response.text)

     

6 Replies

  • hi ashk  I've had spotty success with PATCH but more consistent success with PUT on pool member state changes. also don't think it's the issue but you use the partition on your pool member object but not your pool object. Your json payload looks fine here, but make sure your quotes aren't smart quotes and I like to see spaces between the key/value pairs.

  • Thank You for the response, 

     

    I tried the same and got Method Not Allowed, something similar.  also, fixed the quotes and pairs. 

    Not sure, If I put the same via PostMan its accepting but  not via Py. :( 

    • JRahm's avatar
      JRahm
      Icon for Admin rankAdmin

      you need to format your python dictionary as json...

      json.dumps({"session": "user-disabled"})
      • JRahm's avatar
        JRahm
        Icon for Admin rankAdmin

        also FYI...if you get a working postman sample, you can click the </> icon and find samples in a lot of tools/languages of that specific query.

         

        And the python sample they shared from my working postman PATCH is:

        import requests
        import json
        
        url = "https://ltm15/mgmt/tm/ltm/pool/~Common~nginx-pool/members/~Common~172.16.102.5:80"
        
        payload = json.dumps({
          "session": "user-disabled",
          "state": "user-down"
        })
        headers = {
          'Content-Type': 'application/json',
          'Authorization': 'Basic notsofastmyfriends',
          'Cookie': 'BIGIPAuthCookie=supersecretpassphrasehere; BIGIPAuthUsernameCookie=admin'
        }
        
        response = requests.request("PATCH", url, headers=headers, data=payload)
        
        print(response.text)