Ask a Question

How to add addtional HTTP header to all the Rest Requests

VTest
Occasional Contributor

How to add addtional HTTP header to all the Rest Requests

Hi All,

I have project with about 20 rest services , it was working fine until last week without any issue, With the recent changes in the code All the Rest requests requests addtional HTTP header, "Accept-Language: en;q=0.8" is there any way to directly add this header to all the Rest requests

Or do i need to add this manually to all Requests.

 

Any suggestions would help. Thank you in advance 

4 REPLIES 4
kondasamy
Regular Contributor

I have a script, which would add the additional header field without disturbing the existing headers. Please feed this script in a Groovy test step and execute. This would apply the new header for all the REST request within the project.

 

import com.eviware.soapui.support.types.StringToStringMap 

//Get through all the test steps in the project
testRunner.testCase.testSuite.project.testSuites.each
{
    suite ->
    suite.getValue().testCases.each
    {
        q1->
        q1.getValue().testSteps.each
        {
                       it->
                       if (it.getValue().config.type.equals("restrequest"))
                       {
                               //Get the headers of the current teststep
                               def headers = it.getValue().getHttpRequest().getRequestHeaders()
                               def list = []
                               //Append the new header to the existing list
                               list.add("en;q=0.8")
                               headers["Accept-Language"] = list;
                               //Set the updated header list
                               it.getValue().getHttpRequest().setRequestHeaders(headers)
                       }
        }
    }
}
log.info("************************************************\n Done ! New Header Appended to all teststeps!\n************************************************ ")

Hope this answers your need!

 

Thanks,

Samy

 

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others, Thanks. ↓↓↓

nmrao
Community Hero

 

This can be done in both automated and manual ways.

 

Looks like, you already know how to add the header to each request manually.

 

In order to be able to do it in automated fashion, it requires 'Events' feature which is available  out-of-the-box in Pro editions[ReadyAPI/SoapUI Pro].

 

However, for open source edition, there is an extension available here, please follow the instructions.

 

https://github.com/nmrao/soapuiExtensions

 

Once you followed the instructions, need to add the code given below to in %SOAPUI_HOME%/scripts/RequestStepBeforeSubmit.groovy file to add the custom http header. So that above extension would execute this groovy file before submitting the request.

 

Here is code that goes into above groovy file:

 

if (submit.request.testStep instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep) {
	submit.request.requestHeaders['Accept-Language'] = ['en;q=0.8']
}

The mentioned header will be added run time for each Rest Request Test step.

If you do not want to add any header you may just comment it out or add conditionally as well.

 

 The whole above will just  act as similar to(Pro) SubmitListener.beforeSubmit() and add the above code.

 

 

 

 

 



Regards,
Rao.
anand7892
Occasional Contributor

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. Please open a question in SoapUI Pro forum rather than reviving an old thread.

2. You can use Events feature, Submit beforeStep and add the code.


Regards,
Rao.
cancel
Showing results for 
Search instead for 
Did you mean: