cancel
Showing results for 
Search instead for 
Did you mean: 

Adding a request Header in all rest request in different Suites

SOLVED
Highlighted
Occasional Contributor

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 follow

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Occasional Contributor

Re: Adding a request Header in all rest request in different Suites

@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)
   }
}
}
}

 

View solution in original post

9 REPLIES 9
Highlighted
Community Hero

Re: Adding a request Header in all rest request in different Suites

Like it was mentioned earlier, you need to use Events feature, Submit beforeStep method.


Have you tried it?


Regards,
Rao.
Highlighted
Occasional Contributor

Re: Adding a request Header in all rest request in different Suites

@rao

It would be better if you can give me steps to follow

Highlighted
Community Hero

Re: Adding a request Header in all rest request in different Suites

Please follow the documentation and revert back if any further questions:
https://support.smartbear.com/readyapi/docs/testing/handling-events.html


Regards,
Rao.
Highlighted
Occasional Contributor

Re: Adding a request Header in all rest request in different Suites

@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)
   }
}
}
}

 

View solution in original post

Highlighted
Frequent Contributor

Re: Adding a request Header in all rest request in different Suites

Hi, If its not composite project, I would say just perform find and replace on xml doc and add it where you need it to go for all tests. will that work?

Highlighted
Community Hero

Re: Adding a request Header in all rest request in different Suites

@anand7892,

 

I am a community member, just help community where possible, not support team.

 

Just look at the precision of your original question before commenting about others. I was giving you leads / pointers to write a solution, haven't given you solution.

 

It is your problem, why do you think you wasted your time? Do you expect others time to waster for your problem?

 

Be gentle with your words.

 

Anyways, you could find lot of examples on the net if you just search precisely. Do not think that is waste of time, think of learning, not blame others, help others if you can, at the end solution is more important.

 

Happy for you that you could solve.



Regards,
Rao.
Highlighted
Community Hero

Re: Adding a request Header in all rest request in different Suites

@anand7892,

 

To be more precise, the best solution is to use Events (which I was keep saying from the beginning) than to Groovy Script and execute it.

 

Because:

This does not solve the purpose if you some one adds new test cases or steps.

And not sure if that groovy script goes into which test case? Not a natural fit to any test case as this is special type.

Again groovy script needs to be run explicitly.

 

By the way, the script, you did, loses existing headers which user has added previously

 

 

 

 

Add a event SubmitListener.beforeSubmit with below code:

 

 

https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/readyapi/events/AddHeaderDynamically...

 

When events are used, the respective code gets executed automatically when the event is fired. In your case, when test is run, event gets fired internally, and header gets added automatically.

 

 

EDIT:

 

Above script is moved to below location

 

https://github.com/nmrao/readyAPIGroovyScripts/blob/master/groovy/events/SubmitListener/beforeSubmit...



Regards,
Rao.
Highlighted
Occasional Contributor

Re: Adding a request Header in all rest request in different Suites

Thanks

I was expecting this kind of help.

 

Highlighted
Community Hero

Re: Adding a request Header in all rest request in different Suites

The better question (with details), better the answers.

Appreciate if you can mark it "accept as solution" if you think that is the best solution.



Regards,
Rao.
New Here?
Join us and watch the welcome video:
Read the Latest News
JuneSCNJ
Top Kudoed Authors