Filtering via CURL

I’m dumbfounded as to how I can pass the filtering rules to the API endpoint to properly filter the results.

curl -G http://127.0.0.1:31000/v1/catalog/service/$strAppID
[{"ID":"7b5b7871-14c5-fcee-f8f4-433782d287b4","Node":"consul-consul-server-1","Address":"10.47.0.4","Datacenter":"dc1","TaggedAddresses":{"lan":"10.47.0.4","lan_ipv4":"10.47.0.4","wan":"10.47.0.4","wan_ipv4":"10.47.0.4"},"NodeMeta":{"consul-network-segment":""},"ServiceKind":"","ServiceID":"aas-7575dfcd-tsr9f-aas-b01ccfe9015556541bedd7e96e65de9d","ServiceName":"aas","ServiceTags":["secure=false"],"ServiceAddress":"10.47.0.9","ServiceTaggedAddresses":{"lan_ipv4":{"Address":"10.47.0.9","Port":36656},"wan_ipv4":{"Address":"10.47.0.9","Port":36656}},"ServiceWeights":{"Passing":1,"Warning":1},"ServiceMeta":{},"ServicePort":36656,"ServiceEnableTagOverride":false,"ServiceProxy":{"MeshGateway":{},"Expose":{}},"ServiceConnect":{},"CreateIndex":3596236,"ModifyIndex":3596236},{"ID":"14f28c20-4ee3-72fd-c2e2-a07fdf19b06f","Node":"consul-consul-server-2","Address":"10.44.0.3","Datacenter":"dc1","TaggedAddresses":{"lan":"10.44.0.3","lan_ipv4":"10.44.0.3","wan":"10.44.0.3","wan_ipv4":"10.44.0.3"},"NodeMeta":{"consul-network-segment":""},"ServiceKind":"","ServiceID":"aas-7575dfcd-8q9m4-aas-47e2ec8c03979123de4cd331b07f227f","ServiceName":"aas","ServiceTags":["secure=false"],"ServiceAddress":"10.44.0.7","ServiceTaggedAddresses":{"lan_ipv4":{"Address":"10.44.0.7","Port":35592},"wan_ipv4":{"Address":"10.44.0.7","Port":35592}},"ServiceWeights":{"Passing":1,"Warning":1},"ServiceMeta":{},"ServicePort":35592,"ServiceEnableTagOverride":false,"ServiceProxy":{"MeshGateway":{},"Expose":{}},"ServiceConnect":{},"CreateIndex":4757028,"ModifyIndex":4757028}]

I’ve constructed the following query, but unable to execute it.

curl -G http://127.0.0.1:31000/v1/catalog/service/aas --data-urlencode filter=“aas-7575dfcd-8q9m4-aas-47e2ec8c03979123de4cd331b07f227f in ServiceID”

curl -G http://127.0.0.1:31000/v1/catalog/service/aas --data-urlencode filter=“ServiceID == aas-7575dfcd-8q9m4-aas-47e2ec8c03979123de4cd331b07f227f”

Failed to create boolean expression evaluator: 1:4 (3): no match found, expected: “!=”, “.”, “==”, “[”, [ \t\r\n] or [a-zA-Z0-9_]

What am I missing?

I think you just need to put some quotes around the service ID like so:

curl -G http://127.0.0.1:31000/v1/catalog/service/aas --data-urlencode filter='ServiceID == "aas-7575dfcd-8q9m4-aas-47e2ec8c03979123de4cd331b07f227f"'

The filter language allows not wrapping the value you are matching against in quotes when that value would look roughly like an identifier in Go or various other programming languages. So if you have letters, numbers and underscores only then it wont need quoting. Anything else and you will have to quote it so the parser knows how to process it.

Thanks! That worked! However when i tried to inject the value as variable, it failed to inject it, hence returning empty data set. How do i resolve this?

curl -G http://127.0.0.1:31000/v1/catalog/service/$strAppID --data-urlencode filter=‘ServiceID == “$strTargetConsulID”’

echo curl -G http://127.0.0.1:31000/v1/catalog/service/$strAppID --data-urlencode filter=‘ServiceID == “$strTargetConsulID”’
curl -G http://127.0.0.1:31000/v1/catalog/service/aas --data-urlencode filter=ServiceID == “$strTargetConsulID”

Hi @phr0zenx,

You should be able to resolve this by using double quotes for the filter string, and escaping the quotes around the service ID variable.

curl --get http://127.0.0.1:31000/v1/catalog/service/$strAppID \
     --data-urlencode filter="ServiceID == \"$strTargetConsulID\""

Yes! That is exactly what i needed! Thank you!