Forum Discussion

Hflower's avatar
Hflower
Occasional Contributor
13 years ago

Retrieving the completed REST URL in Groovy

I'm using Template Resource Parameters on my REST Resource and I need to use the completed the URL that is sent in the HTTP Header as part of my authentication routine.

My endpoint is http://www.company.com
My Resource Path is /ws/app/search/{Query}

http://www.company.com/ws/app/search/Query

If my query string is acme

I need http://www.company.com/ws/app/search/acme

The template substitution is working ok but I was wondering if I can get the complete URL in Groovy?
I've had a look through the soapUI AP docs but get a bit lost
I've written some code that can work with QUERY type parameters, but now I want to test with a different type of URL

httpRequest = testStep.testRequest
propertyHolder = httpRequest.getParams()

if(propertyHolder){
queryString = getQueryString(httpRequest,propertyHolder)
}else{
queryString = ""
}

def getQueryString(httpRequest,propertyHolder){
queryString = "?"
for( properties in propertyHolder){
//log.info "Name is ${properties.key}"
value1 = properties.key
value = httpRequest.getProperty("${value1}").getValue()
if(value){
if(queryString.length() == 1){
queryString = queryString + properties.key + "=" + value
}else{
queryString = queryString + "&" + properties.key + "=" + value
}
}
}
log.info "queryString is $queryString"
return queryString
}

15 Replies

  • Hflower's avatar
    Hflower
    Occasional Contributor
    (this thread isn't resolved properly yet, but I can't find the edit Title link)

    the requestUri trick only works if you use a Templated variable URI >:(

    If you have parameter query type variables then I'm back to finding or defining the proper/consistant sort order for them before I calculate the signature for my REST request.

    It's not always alphabetic and I haven't found where it's stored (if anywhere)
  • What if you in a RequestFilter.filterRequest add:

    log.info context.httpMethod.getURI()


    --
    Regards

    Erik
    SmartBear Sweden
  • Hflower's avatar
    Hflower
    Occasional Contributor
    YES!

    that's it Erik, thank you so much

    I had manged to sort() the params value before I used them, but I haven't tested enough URIs to be sure it always comes out alphabetically.

    requestPath = context.getProperty( "requestUri" )
    //log.info "requestPath is ${requestPath}"
    pathToSign = requestPath.URI.toString()
    params = context.getProperty("httpResponseProperties")
    //log.info "Params is ${params}"

    path = request.getPath();
    //log.info "Path is ${path}"
    if ( path =~ '''}'''){
    //log.info("Do Nothing")
    }else{

    def queryString = "?"
    request.params.sort().each{ param ->
    if((queryString.length() != 1) && (param.value.value != '') )
    queryString += "&${param.value.name}=${param.value.value}"

    //testString = "${param.value.value}"
    else if (param.value.value != ''){
    queryString += "${param.value.name}=${param.value.value}"
    }
    }
    pathToSign += queryString
    }
  • Nice to hear that it helped.
    Let me know if you have any other issues.

    --
    Regards

    Erik
    SmartBear Sweden