Forum Discussion

SiKing's avatar
SiKing
Community Expert
5 years ago
Solved

add request header to all calls

I am trying to find a better solution for adding a header to all calls. This questions was already asked here and earlier here. The proposed solution for both of these was to add a RequestFilter.fil...
  • SiKing's avatar
    5 years ago

    RequestFilter.filterRequest event:

    // add header only for "interesting" service
    if(request.operation.interface.name != "interesting/service")
    	return
    
    def headers = request.requestHeaders
    headers.put("X-Test", "random string")
    request.requestHeaders = headers
    

    RequestFilter.afterRequest event:

    // look for header only for "interesting" service
    if(request.operation.interface.name != "interesting/service")
    	return
    
    def headers = request.requestHeaders
    if(headers.containsKey("X-Test"))
    {
    	// request.requestHeaders.clear() does not work here! 
    	headers.remove("X-Test")
    	request.requestHeaders = headers
    }
    

    I am using this to add an "Authorization" header to all my requests. Only the service of interest gets the Auth header, and afterwards it gets cleared out so nothing goes into SCM.

    Word of caution: SoapUI (and ReadyAPI) have a long standing bug: If you have many testcases running in parallel, the events could lose track of things and fire all over the place.