Forum Discussion

MarcovS's avatar
MarcovS
Contributor
9 years ago

Add HTTP headerfield to Requests

Hi All,

 

I'm using Ready API 1.5.0 for testing REST API calls. Came across this Groovy-script that seems to be useful to add a HTTP-header field to all teststeps. I'm executing it on Project-level fot all TestSuites and TestSteps in that area. The remark that it only should work in 3.5.1 sounds a bit odd to me; executing the script results in the error attached to this message. How can I get this script to work properly in SOAP UI NG?

 

// Add HTTP headers to Requests
// NOTE: will work only in 3.5.1 version

import com.eviware.soapui.impl.wsdl.teststeps.*
import com.eviware.soapui.support.types.StringToStringMap;

def tc = testRunner.testCase.testSuite.getTestCaseByName("<TESTCASE_NAME>")
def ts = tc.getTestStepByName("<TESTSTEP_NAME>")

def headers = new StringToStringMap()

headers.putIfMissing("PmAuthenticationToken","<TOKEN>")
log.info ts.getHttpRequest().setRequestHeaders(headers)

 

  • nmrao's avatar
    nmrao
    9 years ago
    def token = new File('C:/temp/SOAPUI/PmAuthenticationToken.txt').text
    if (token) {
       request.requestHeaders['PmAuthenticationToken'] = [token]
    } else {
       log.error("Token is empty or null")
    }
  • nmrao's avatar
    nmrao
    Champion Level 3
    You mentioned header needs to be added for the test steps which is not possible if the test step type is jdbc/groovy etc.
    So, the question is that please specify if test step is SOAP? or REST or HTTP Request or something else?
    • MarcovS's avatar
      MarcovS
      Contributor

      I mentioned in my introduction "..testing REST API calls.." and thats still the case. ;-)

       

      To be more specific: they are REST Request Test Steps. And because I've about 200 different REST Request Test Steps divided into several (functional) blocks/projects. Also useful, because we're working with more people on these sources.

      • nmrao's avatar
        nmrao
        Champion Level 3
        Ok. If that is the case, then you can use Events feature to handle your case like RequestFilter, add headers before run for each rest so that you do not have to really worry about the number of test cases that it needs to be updated.