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
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
thanks that works too
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.