Forum Discussion

anand7892's avatar
anand7892
Occasional Contributor
8 years ago

Adding a request Header in all rest request in different Suites

I want to add a header in all the rest Request in my project The value that i need to put in the header is getting generated by login API.   I am using Pro Version Can you suggest me steps to fol...
  • anand7892's avatar
    anand7892
    8 years ago

    nmrao

    You could have given me more precise solution to save my time but seems like you are here to refer to only urls of SOAPUI

     

    So i wasted my few hours and find a better solution:

     

    Here is more precise solution to this:

    Problem: I wanted to add a header with name "Authorization" and value "${#project#token}"(stored at project level)'

     

    Solution:

    Run Groovy script and it will solve your problem.

     

     

    import com.eviware.soapui.impl.wsdl.teststeps.*
    import com.eviware.soapui.support.types.StringToStringMap 
    
    def project = context.testCase.testSuite.project
      
    def token = "\${"+"#"+"Project"+"#"+"token}"
    log.info(token)
    def valueOf = { key -> testSuite.getPropertyValue( key ) }
    project.testSuiteList.each{ testSuite ->
    testSuite.testCaseList.each { testCase ->
       testCase.testStepList.each { testStep ->
          if( testStep instanceof WsdlTestRequestStep || testStep instanceof RestTestRequestStep  ) 
          {
          	def headers = new StringToStringMap()
    		headers.put("Authorization",token)
             log.info("Setting HTTP headers ${headers} in test case ${testCase.label}")
             testStep.testRequest.setRequestHeaders(headers)
       }
    }
    }
    }