Forum Discussion

Ameer's avatar
Ameer
New Contributor
10 years ago

How to add Authorization Header to a Rest Request in Java

Hi,

 

I am newbie to SOAP UI java Api's. Could you please help me on setting Authorization Header to a Rest Request for a test suite in java. 

 

Please find the Step:

 

WsdlProject wadlProject = new WsdlProject();

WsdlTestSuite testSuite = wadlProject .addNewTestSuite("Sample Test");

WsdlTestCase testCase = testSuite.addNewTestCase("Sample Test Case");

WsdlTestStep testStep = testCase.addTestStep("httprequest","Valid","requestURL","GET");

TestCaseRunner testCaseRunner = new WsdlTestCaseRunner((WsdlTestCase)testCase, null);

TestCaseRunContext  testStepContext = new WsdlTestRunContext(testStep);    

testStep.run(testCaseRunner, testStepContext);

 

I am not sure how to set the Authorization header in the above code. Could you please help

 

 

 

  • Same way it is! Here is a sample snippet for your reference,

    import com.eviware.soapui.support.types.StringToStringMap 
    def headers = new StringToStringMap()
    headers.put("Header1","Value 1")
    headers.put("Header2","Value 2")
    headers.put("Authorization","Basic fgfhgfdhCGDGRSGFEWRGFBGHLI")
    testCaseRunner.testCase.getTestStepAt(1).testRequest.setRequestHeaders(headers)

    Thanks,

    Samy

     

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

  • kondasamy's avatar
    kondasamy
    Regular Contributor

    If I am correct, I guess you are integrating SoapUI using some IDE or with some third party tools as mentioned here - http://www.soapui.org/developers-corner/integrating-with-soapui.html

     

    BTW.,You can set UserName and Password (Basic Authentication) using either way through testCaseRunner or testStepContext. For instance here is a sample snippet which make use of testCaseRunner,

    testCaseRunner.testCase.getTestStepAt(1).testRequest.setUserName("User1")
    testCaseRunner.testCase.getTestStepAt(1).testRequest.setPassword("pwd")

     

    Thanks,

    Samy

     

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

    • Ameer's avatar
      Ameer
      New Contributor

      Hi Samy,

       

      Yes, you are correct. I am integrating soap ui with my JUNIT.

       

      Thanks for your reply. But, I want to pass Authorization details via http headers. Could you please help.

       

       

      Thanks 

      Ameer

      • kondasamy's avatar
        kondasamy
        Regular Contributor

        Same way it is! Here is a sample snippet for your reference,

        import com.eviware.soapui.support.types.StringToStringMap 
        def headers = new StringToStringMap()
        headers.put("Header1","Value 1")
        headers.put("Header2","Value 2")
        headers.put("Authorization","Basic fgfhgfdhCGDGRSGFEWRGFBGHLI")
        testCaseRunner.testCase.getTestStepAt(1).testRequest.setRequestHeaders(headers)

        Thanks,

        Samy

         

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