Having Troubles with Roles and Filters

I’m using Web APIs using the Data API feature provided by MongoDB. I’m now trying to make the roles and filters work, but when using “Apply When” or filter by documents, I’m unable to get the Expansion “%%request” to work.

What I’m trying to do, is add filters based on request headers or request actions, as follows:
“%%request.action”: “aggregate”
also tried:
“%%request.requestHeaders.customHeader”: “CustomValue”

each of them was attempted separately, but none of them seemed to work.

Same thing happened when trying to filter documents by having a document attribute equal a request header.

This is crucial to my app, so can someone please help me with this?

Thanks in advance.

Hi @Diaa_tamimi - do you mind sending me the structure of the request you send to the Data API (you can redact the sensitive data)

And just confirming the changes were deployed once you defined this rule configuration.

URL: https://data.mongodb-api.com/app/--------------/endpoint/data/beta/action/aggregate
Headers: content-type, api-key, and Code (which is a custom header i’m using to pass a request value)

In rules and filters, under “apply when”, i’m using the below statement:
“%%request.action”: “aggregate”
Body is as per the Data API documentation, and the request is working fine, i’m only having a problem with the rules “apply when” section

it doesn’t seem to apply, but when i try something like “%%request.action”: {“$regex”:“aggregate”} it works. i don’t want to have to use $regex because MongoDB then ‘automatically’ changes it to pattern/option and when that happens it stops working as well.

So then i tried using the expression:
“%%request.requestHeaders.Code”: “Value”
also doesn’t work

Hi @diaa_tamimi

Got it, the data api is a little bit odd in the sense that the name for the action is actually data/v1/aggregate I will make sure to follow up with docs about this to explicitly call this out

For the headers, I’m not sure what’s going on but let me follow up on that in a little while

Great. Thanks for your time and looking forward to your feedback

I’ve just checked, and data/v1/aggregate is working.
Now I just need to be able to use the headers in my rules/filters and need to know how to call their value.

Again, thanks for your time

Dear @Sumedha_Mehta1 ,

Any updates? I really need to be able to call header values in Rules and Filters.


Hi - thanks for waiting. All of the headers actually consist of array values, so you would actually have to check if your header value is in the array. If the length of the array is 1, you should be able to do something like this:

  "%%request.requestHeaders.Content-Type.0": "application/json"

or using $in will work too

   "application/json": { "$in": "%%request.requestHeaders.Content-Type" }

Thanks for the information. I’ll test it as soon as possible and let you know