Forum Discussion

suchindrac's avatar
suchindrac
Occasional Contributor
15 years ago

How to set a header in a SOAP request in SOAP UI using Groovy Script?

Hi All,
          I have done the following in Groovy Script:

import java.util.*;
import org.apache.commons.codec.binary.Base64



def dialog = com.eviware.soapui.support.UISupport.createConfigurationDialog( "CIM User Login Credentials" );
dialog.addTextField( "UserName", "Username of CIM User" );
dialog.addTextField( "Password", "Password of CIM User" );
def map = new java.util.HashMap();
def authMap = new java.util.HashMap();

map.put( "UserName", "" );
map.put( "Password", "" );

if ( dialog.show( map ) )
{
     
      byte[] username = map.get ( "UserName" ).toString().getBytes();
      byte[] password = map.get ( "Password" ).toString().getBytes();
      byte[] authString = new byte[username.length + password.length + 1];

      System.arraycopy(username, 0, authString, 0, username.length);
      authString[username.length] = (byte) ':';
      System.arraycopy(password, 0, authString, username.length+1, password.length);

      eString = authString.encodeBase64()
      finalHeader = "Basic " + eString

      log.info "Base64 Encoded String: " + eString
     
      def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
      def httpReqHeaders = context.testCase.testSteps["CIM Login Request"].testRequest.requestHeaders

      //def httpResponseHeaders = context.testCase.testSteps["Google Search request"].testRequest.response.responseHeaders
               
      def authHeader = httpReqHeaders["Authorization"]
      log.info "Present Authorization Header: " + authHeader
      log.info "Setting Authorization Header to " + eString

      authMap.put( "Authorization" , finalHeader);

      //groovyUtils.setPropertyValue("CIM Login Request", "Authorization", eString.toString())
httpReqHeaders.put("Authorization", finalHeader)

log.info "Authorization Header after modification: " + httpReqHeaders["Authorization"]



}
else testRunner.cancel( "No Credentials input" );

However i think there is an updateHeaders or something like that because after running the test case, i am unable to see the updated headers in "CIM Login Request"