List budget policies applying filter_by

fkseki
New Contributor III

I'm trying to list budget policies using the parameter "filter_by" to filter policies that start with "aaaa" but I'm getting an error  "400 Bad Request"

{'error_code': 'MALFORMED_REQUEST', 'message': "Could not parse request object: Expected 'START_OBJECT' not 'VALUE_STRING'\n at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 15]\n at [Source: UNKNOWN; line: 1, column: 15]", 'details': [{'@type': 'type.googleapis.com/google.rpc.RequestInfo', 'request_id': '525465a2-3c47-45f1-8528-523b2a4f77b0', 'serving_data': ''}]}

 Using:

url = https://accounts.azuredatabricks.net/api/2.1/accounts/<account_id>/budget-policies

headers = {
'Authorization': f'Bearer <TOKEN>',
'Accept': 'application/json'
}
params = {"filter_by":{"policy_name":"aaaa"}}
response = requests.get(url=url, headers=headers, params=params)

Is this parameter functional?

using as reference: https://docs.databricks.com/api/azure/account/budgetpolicy/list

 

szymon_dybczak
Esteemed Contributor III

Hi @fkseki ,

Since filter_by is an object parameter in a GET request, I think that you need to convert it to a JSON string when passing it as a query parameter. Try to do something like this:

filter_by_json = json.dumps({"policy_name": "aaaa"})
params = {"filter_by": filter_by_json}
response = requests.get(url=url, headers=headers, params=params)

lingareddy_Alva
Honored Contributor III

Hi @fkseki 

The filter_by parameter is functional, but the error you’re seeing is because you’re passing it as a
Python dict in params — which requests will serialize into query string format — whereas the API
expects a JSON object in the request body for this particular endpoint.

For the list call in the Databricks Budget Policy API, filter_by isn’t a plain query parameter; it’s part
of the request payload (POST), even though it’s a “list” operation

import requests
import json

url = f"https://accounts.azuredatabricks.net/api/2.1/accounts/{account_id}/budget-policies/list"

headers = {
    "Authorization": f"Bearer {TOKEN}",
    "Accept": "application/json",
    "Content-Type": "application/json"
}

payload = {
    "filter_by": {
        "policy_name": "aaaa"
    }
}

response = requests.post(url, headers=headers, data=json.dumps(payload))
print(response.status_code, response.text)

Key differences from your attempt:
The method should be POST, not GET.
The filter_by object should be in the request body as JSON, not in params.
The URL is usually <...>/budget-policies/list rather than just /budget-policies.

 

 

 

LR

fkseki
New Contributor III

Thanks for the reply, @szymon_dybczak and @lingareddy_Alva.

I tried both approaches but none was successful.

url = f'{account_url}/api/2.1/accounts/{account_id}/budget-policies'

filter_by_json = json.dumps({"policy_name": "aaaa"})
params = {"filter_by": filter_by_json}

headers = {
'Authorization': f'Bearer {TOKEN}',
'Accept': 'application/json',
'Content-Type': 'application/json'
}

response = requests.get(url=url, headers=headers, params=params)
params = {"filter_by":{"policy_name":"aaes"}}
response = requests.post(url=url, headers=headers, data=json.dumps(params))

For the post method, I tried with and without the "/list" at the end of the endpoint.

With it I got an ENDPOINT_NOT_FOUND error

Without it, the INVALID_PARAMETER_VALUE error, probably because this endpoint with POST method is to be used to create a new budget policy

'error_code': 'INVALID_PARAMETER_VALUE', 'message': 'Invalid policyName - policy name cannot be empty'

 

szymon_dybczak
Esteemed Contributor III

Hi @fkseki ,

Oh man, after 1,5 hour of trail and error I've found a way to make it work 😄 You need to use dot notation and it will work. They shoud've specified this in docs though...

params = {"filter_by.policy_name": "szymon - genric"}

headers = {
 'Authorization': f'Bearer {TOKEN}',
 'Accept': 'application/json',
 'Content-Type': 'application/json'
}

response = requests.get(url=url, headers=headers, params=params)

data = response.json()
print("SUCCESS!")
print(json.dumps(data, indent=2))

 

View solution in original post

Wonderful! It works! Thank you very much, I tried many different ways but didn't think to test this one, though.

szymon_dybczak
Esteemed Contributor III

No problem @fkseki. It was interesting, but at the same time frustrating experience 😄 Good that we managed to solve it.

@szymon_dybczak, you are a legend, saved me a lot of time.