cancel
Showing results for 
Search instead for 
Did you mean: 

Add http Headers at Step - Java

Highlighted
Occasional Contributor

Add http Headers at Step - Java

Hello,
We are trying to Automate our Suite from a Java based application. We have referred SOAPUI 5.2.1 libs in our project.
So far, I am able to control execution of Suite from my app.
We are facing challenges while Adding Headers (authorization headers) to the requests (at WsdlTestStep level).
We searched on google, but could not find a way to add http headers.

Following code sample matches with our requirement, but could not find equivalent APIs for Java. (believe its in groovy, we need Java)
https://community.smartbear.com/t5/SoapUI-NG/How-to-Add-HTTP-Headers-to-test-request-using-API/td-p/...
https://community.smartbear.com/t5/SoapUI-Open-Source/Using-Groovy-to-set-a-HTTP-header-value/td-p/1...

 

Our code:
WsdlTestStep testStep = objectcase.addTestStep("httprequest","Valid","my REST url","GET");
testStep.setPropertyValue("Authorization", "Bearer xxx"); // tried this, didn't work

TestCaseRunner testCaseRunner = new WsdlTestCaseRunner((WsdlTestCase)objectcase, null);
TestCaseRunContext testStepContext = new WsdlTestRunContext(testStep);
testCaseRunner.getTestCase().getTestStepAt(0).setPropertyValue("Authorization", "Bearer xxx");
testStep.run(testCaseRunner, testStepContext);

Referring to https://community.smartbear.com/t5/SoapUI-Open-Source/How-to-add-Authorization-Header-to-a-Rest-Requ...
It says we should get testRequest object from step, but I could not find any method with 'Request' label.

Could you please help me to understand how to retrieve httpRequest from Step using Java. Or alternate approach to set http headers.

 

Thanks

 

Tags (2)
1 REPLY 1
Highlighted
Occasional Contributor

Re: Add http Headers at Step - Java

I am able to add headers, sharing if required by anyone .

 

StringToStringMap headers = new StringToStringMap();
headers.put("Authorization", "Basic xxx");
HttpTestRequestStep testStep = (HttpTestRequestStep)objectcase.addTestStep("httprequest","Valid","token url","POST");
testStep.getHttpRequest().setRequestHeaders(headers);
testStep.setName("xxx");
TestCaseRunner testCaseRunner = new WsdlTestCaseRunner((WsdlTestCase)objectcase, null);
testStepContext = new WsdlTestRunContext(testStep);

// testStep.run(testCaseRunner, testStepContext);
testCaseRunner.runTestStepByName("zipcode");

 

System.out.println("JSON Response from server"+testStepContext.expand("${#Response}"));

New Here?
Join us and watch the welcome video:
Building with the Collaborator API

Help Documenation
The Collaborator API is a JSON-RPC API that lets you integrate with Collaborator 9.0 and above. For more information about how the API works, check out the API description here:


Sample Plugins
For an example of an add-in that uses the API, take a look at the Microsoft Word Add-in for Collaborator:


We list all the great plugins our network has already built on this page on the SmartBear website.

Description
SBC2020