How to get the full build url including query filter

serge calderara
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 21, 2022

Dear all,

I have the following code method which is calling an Ms Graph API :

public String getGuestUserId(String AuthToken,String userEmail){

    String _userId
    def http = new HTTPBuilder("https://graph.microsoft.com/v1.0/users/?")

        http.request(GET) {

            requestContentType = ContentType.JSON
            //uri.query = [ $filter:"mail eq '$userEmail'"].toString()
            uri.query=[$filter:"mail eq '$userEmail'"]
           
            headers.'Authorization' = "Bearer " + AuthToken    

            response.success = { resp, json ->
                if (**json.value**){
                _userId=**json.value**[0].id    
                
                } 
                else
                _userId=-1 // user does not exist
                                    
            }

            // user ID not found : error 404
            response.'404' = { resp ->       
                _userId = 'Not Found'
            }

        }
        _userId
        
    } 

When this method is called, I need to log the full URL which is executed in order to verify it

I have try http.GetUri() but it return only the uri part and I need to get as well the querry filter

My expected result should be something like below :


https://graph.microsoft.com/v1.0/users?$filter=mail eq 'user@xx.onmicrosoft.com'

How to get that full url in my code to verify that syntax is as expected ?

 

Regards

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 21, 2022

Define a variable outside of the request to hold your url.

While in the request, after updating the uri (instance of groovyx.net.http.UriBuilder) with path and or query, load the fullUri variable so you can examine it after the request is complete.

But I think I see where you have an error. You don't want to include the '?' in the defaultURI. The Uribuilder will know how to handle the query items (including the ? and & for separation).

Try like this:

public String getGuestUserId(String AuthToken, String userEmail) {

String _userId
def http = new HTTPBuilder("https://graph.microsoft.com/")
def fullUri
http.request(GET) {

requestContentType = ContentType.JSON

uri.path = 'v1.0/users/'
uri.query = [$filter: "mail eq '$userEmail'"]
fullUri = uri.toString()

headers.'Authorization' = "Bearer " + AuthToken

response.success = { resp, json ->
if ( ** json.value** ) {
_userId =**json.value**[0].id
} else _userId = -1 // user does not exist
}
// user ID not found : error 404
response.'404' = { resp -> _userId = 'Not Found' }
}
log.info "Full url=$fullUri"
_userId

}

You can include the path in the defaultURI, or you can specify it in the uri variable like with the query.

The following two are equivalent:

def uri = new URIBuilder('https://graph.microsoft.com/v1.0/')
uri.query = ['q':123, $test:'abc']
uri.path = 'users'

def uri2 = new URIBuilder('https://graph.microsoft.com/v1.0/users')
uri2.query = ['q':123, $test:'abc']

assert uri ==uri2
serge calderara
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 25, 2022

thanks that works too

TAGS
AUG Leaders

Atlassian Community Events