1
0
-1

What is the best approach to create a Client to call a REST Webservice? Using Transaction Manager or Java Script in Composer? I tried using Transaction Manager, but the GetRequest Class had issues in Groovy Script.

The Avoka TM Version we are using is 4.0.7

Build Number 37608

Number of Active Forms: 8

Thanks,
Ramya

    CommentAdd your comment...

    1 answer

    1.  
      2
      1
      0

      Hey Ramya, in most cases developing a Groovy service on Transaction Manager to call a REST service is best approach and then having the client (form) make a Dynamic Data call to the Groovy service. Looking a the documentation for com.avoka.component.http.GetRequest - this class was introduced in version 4.2.0 so would not be available in your version.

      Note that this class wraps up the Apache HTTP Components library for greater convenience, so you should be able to achieve what you want by using the Apache HTTP classes directly.

      Given the age of your environment I would recommend giving strong consideration to upgrading if you haven't already.

      Kind regards, Ben.

      1. Ramya Tata

        Thank you Ben for your response. I am trying to call a REST API using below code in DDS. I need to get a Cookie-Session ID as Response. But instead get the Error. Any suggestions what I am missing adding into the headers.

        Thanks,

        Ramya

        import com.avoka.fc.core.service.*  
        import org.apache.http.*
        import org.apache.http.client.methods.HttpPost
        import org.apache.http.impl.client.DefaultHttpClient
        import org.apache.http.util.EntityUtils
        import java.util.List
        import java.util.LinkedHashMap;
        import java.util.Map;


        // Get a reference to the event log
        def eventLogService = new com.avoka.fc.core.service.EventLogService()

        // Log the event that the service has started
        eventLogService.logInfoEvent(serviceDefinition.serviceName + ' Started')

        def httpClient = new DefaultHttpClient();
        def postRequest = new HttpPost("https://novatus-dev.qualcomm.com/Contracts/rest/v1/session?username=xxx&password=yyy");
        postRequest.addHeader("accept", "application/x-www-form-urlencoded");
        postRequest.addHeader("Access-Control-Allow-Origin", "*");
        postRequest.addHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS" );
        postRequest.addHeader("Access-Control-Max-Age", "3600" );
        postRequest.addHeader("Set-Cookie", "");

        def response = httpClient.execute(postRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            eventLogService.logInfoEvent("Failed : HTTP error code : "+ response.getStatusLine().getStatusCode());
        }

        def br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        def output;
        eventLogService.logInfoEvent("Output from Server .... \n");

        while ((output = br.readLine()) != null) {
            eventLogService.logInfoEvent(output);
        }

        eventLogService.logInfoEvent("response.getStatusLine(): " + response.getStatusLine().getStatusCode());
        eventLogService.logInfoEvent("response.getHeaders(): " + response.getHeaders());
        eventLogService.logInfoEvent("response.getFirstHeader('Set-Cookie'): " + response.getFirstHeader('Set-Cookie'));
        eventLogService.logInfoEvent("response.getLastHeader('Set-Cookie'): " + response.getLastHeader('Set-Cookie'));
        eventLogService.logInfoEvent("response.getEntity().getContent(): " + response.getEntity().getContent());

        httpClient.getConnectionManager().shutdown();
        // Log the event that the service has stopped
        eventLogService.logInfoEvent(serviceDefinition.serviceName + ' Stopped')
        if(response != null)
            return "Failed : HTTP error code : "+ response;


      2. Ben Warner

        If you google HTTP Error code 406 you will find plenty of information e.g.

        https://www.inmotionhosting.com/support/website/error-numbers/406-error

        The issue seems to be related to your 'accept' header. Try removing it.

      3. Ramya Tata

        Thank you Ben. Removing 'Accept' Header worked.

        Ramya

      CommentAdd your comment...