如何为API提供的Curl GET命令添加过滤(filter)参数?
Hey there! Let's get that filter parameter working in your curl command—it's actually pretty straightforward once you handle the shell's quote escaping correctly.
Here's the Modified Command
Since you already know the filter works in Postman (?filter=userName co "random.user"), you just need to append that query string to the URL in your curl command. The key thing to watch out for is the double quotes around "random.user"—in a shell environment, these need to be escaped so the command doesn't break.
Here's the updated curl command:
curl -k -X GET -H "Authorization: Bearer $AccessToken" "https://xx/admin/v1/Users?filter=userName co \"random.user\"" -o search_results.json
Why This Works
- We're keeping the URL wrapped in double quotes so the shell correctly expands the
$AccessTokenvariable (if we used single quotes here, the variable wouldn't be replaced with your actual token). - The
\"escapes the inner double quotes around"random.user", telling the shell that these quotes are part of the URL query string, not the end of the URL's quoted string.
Alternative: URL-Encoded Spaces (Optional)
If you prefer, you can replace the spaces in the filter with %20 (the URL-encoded version of a space) instead of relying on the shell to handle spaces in the quoted URL. This is also valid:
curl -k -X GET -H "Authorization: Bearer $AccessToken" "https://xx/admin/v1/Users?filter=userName%20co%20\"random.user\"" -o search_results.json
Either version should work exactly like your Postman request, returning the filtered user data.
内容的提问来源于stack exchange,提问作者Rohit Prasad

