Forum Discussion

TDYSmartBear's avatar
TDYSmartBear
Occasional Contributor
7 years ago

Use regular expression on ReadyAPI event target

I'm trying to use different RequestFilter.filterRequest to add headers to different tests. Not sure how the regular expression should be used in the target field.   I'd like to use RequestFilter1 f...
  • Bill_In_Irvine's avatar
    Bill_In_Irvine
    7 years ago

    I am using ReadyAPI 2.2.0 and haven't upgraded yet.  I don't see a "event dialogue" so I'm not sure what you mean. But From the Events button at the upper right, next to the bell icon, click the button.

     

    The Events window opens. Use "+" to add RequestFilter.filterRequest - I thought from your original message you already know this stuff. 

     

    In your groovy script - assuming you use the GET processing like the above code I suggested,

     

    def headers = request.requestHeaders
    
    String theMethod = context.getProperty("httpMethod").toString()
    
    String theGet = new String("GET")
    
    def namelist = context.getPropertyNames()
    
    // EXAMPLE: Set header to application/x-www-form-urlencoded in case GET
    
    // clear out request content, set header Content-Type to application/x-www-form/urlencoded
    if (theMethod.substring(0, 3) == theGet) 
    {
    
         // Clear out the request content. Actually this probably fullfills the same
         // as "Remove Empty Content" in the REST Request Properties: tab
         context.setProperty("requestContent", '')
         request.requestContent = ''
    
    	log.info "headers (right before adding  the urlencoded content type): " + headers
    
    	log.info "headers size now: " + (request.requestHeaders.size())
    
    	headers.put( "Content-Type", "application/x-www-form-urlencoded") // Replace values with those you need
    
    	log.info "Added Content-Type: application/x-www-form-urlencoded"
    
            // finish setting the header...
    	request.requestHeaders = headers
    
    	log.info "headers size now: " + (request.requestHeaders.size())
    
    	// display the header
    	log.info "actual header is " + request.requestHeaders
    
    }
    else
    {
    //blah blah blah 2
    }