CMDBuild Forum

WsQueryOptions in REST API V3

Hi CMDBuild Team,

I want to use WsQueryOptions in a request to “Cards” > “readmany” in REST API V3.

I am trying to use WsQueryOptions, but it does not work as expected.
Can you tell me how to specify the “filterStr” parameter?

↓This returned the expected result.

url = '...services/rest/v3/classes/Account/cards'
params = {
    "limit": 2,
    "attrs": ['Description', 'Code'],
    # "filterStr": "Description=something"
}
res = cmdbuild.request_get(url=url, params=params)
print(res.text)
```

However, ↓this was not filtered out by filterStr.

```
url = '...services/rest/v3/classes/Account/cards'.
params = {
    "limit": 2,
    "attrs": ['Description', 'Code'],
    "filterStr": "Description=something"
}
res = cmdbuild.request_get(url=url, params=params)
print(res.text)
```

I tried several patterns, but could not find a valid expression.
It would be appreciated if you could tell me how to make a valid request.


Thank you.
Mizuki

Hi Mizuki,
I struggled with this for several days and tried everything I could think of. In the end I solved it using query language and urlencode. Weird, but the standard key=value syntax you are using just didn’t work for me no matter what I did.
This is the query structure that worked for me in the end (just an example based on your code - won’t be 100% right but you get the idea):
{“attribute”:“simple”,“operator”:“equal”,attribute:“Description”,“value”:“something”}
Then urlencode it so that you get something like this (truncated as it’s a long string):
%7B%22attribute%22%3A%20%7B%22simple%22%3A%20%7B%22operator%22… etc
Then use that as your filter argument.
One thing to note, I did not use filterStr as that just wouldn’t work. Using filter as the keyword did.
Good luck.

1 Like

Hi shanew,

Thank you so much for your kind reply.

I used the “simple filter” syntax described on page 49 of this document, and finally it worked!
https:// usermanual.wiki/Document/CMDBuildTechnicalManualENGV240.1276356727/html
↑I can’t include URL in my post, so please eliminate space follows “https://” .

The request is in this form.

filter = {
    "attribute": {
        "simple": {
            "operator": "equal",
            "attribute": "Description",
            "value": "something"
        }
    }
}
1 Like

I used the “simple filter” syntax described on page 49 of this document
I’m sorry, it was a mistake of page 41.

Hi Mizuki and shanew,

I am struggling with the same problem but I have not yet successfully found a way to put your solution into working python code.

Could you share a bit more of your code showing how you put together the “params” variable for the request?

Thanks!

Hi vanburik,

This code (it’s Python) worked for me.

# variables
url_base =  'https://endpoint/services/rest/v3'
classname = 'Office'
operator = 'equals'
attribute = 'Description'
value = 'something'

url = f'{url_base}/classes/{classname}/cards'

# set parameter
filter_param = {
    "attribute": {
        "simple": {
            "operator": operator
            "attribute": attribute,
            "value": value
        }
    }
}
params = {"filter": json.dumps(filter_param)}

# request
cards = requests.get(
    url,
    headers=self.headers,
    params=params
    )

Good luck!

1 Like

awesome, thank you very much!

1 Like

Trying to run tests in SOAPUI on the REST endpoints using a filter defined in openMAINT (‘class filter’)as a parameter but cannot get the filter to work no matter the format of the string value. Am I incorrect in my thinking here? I understand you can create the filter in a JSON using the simple syntax mentioned in this thread but has anyone been able to use a ‘class filter’ in a request or are theses only useful in the openMAINT UI?

I was able to run requests with complex search filters, I believe I was incorrect in thinking I could use the openMAINT class filters. The format and syntax for the filter is:

{'attribute':
 {'and': [
    {'simple':
         {'operator': 'between', 'attribute': '{your-attribute}',
          'value': ['{value1}', '{value2}'], 'parameterType': 'fixed'}},
 {'and': [
    {'simple':
         {'operator': 'equal', 'attribute': '{your-attribute}', 'value': ['{your-value}'], 'parameterType': 'fixed'}
        }
      ]
    }
  ]
 }
}

This is just an example, adjust to your needs by changing the ‘operator’ and ‘attribute’ values. Be sure to insert your search parameters in place of ‘{your-attribute}’ and ‘{your-value}’

1 Like