Service Virtualization

 View Only
Expand all | Collapse all

Methods supported in REST Step

Jump to Best Answer
  • 1.  Methods supported in REST Step

    Posted Aug 23, 2016 11:04 AM

    Do any of the Devtest versions support "PATCH" method in "REST STEP" in CA Application Test.



  • 2.  Re: Methods supported in REST Step

    Posted Aug 23, 2016 11:24 AM

    HEAD and PATCH are not supported at present , please create an idea on communities to add this features on roadmap.

     

     

    Thanks,

    Vamsi



  • 3.  Re: Methods supported in REST Step

    Posted Aug 24, 2016 01:11 AM

    Hi Vamsi,

     

    Thanks for the update.

    Could you please let us know if there is any workaround or alternate way to implement PATCH method in Devtest.



  • 4.  Re: Methods supported in REST Step

    Posted Aug 24, 2016 01:30 AM

    Can you try , send it as POST request and add query parameter _HttpMethod=PATCH



  • 5.  Re: Methods supported in REST Step
    Best Answer

    Posted Aug 24, 2016 02:29 AM

    OR You can add java script step with the following code :

     

    import org.apache.http.HttpEntity;

    import org.apache.http.client.ClientProtocolException;

    import org.apache.http.client.methods.CloseableHttpResponse;

    import org.apache.http.client.methods.HttpPatch;

    import org.apache.http.impl.client.CloseableHttpClient;

    import org.apache.http.impl.client.HttpClients;

    import org.apache.http.util.EntityUtils;

    StringBuilder builder = new StringBuilder();

     

      CloseableHttpResponse response = null;

     

      try {

     

      HttpPatch patch = new HttpPatch("http://localhost:6001/login/authToken");

     

      patch.setHeader("Authorization", "Basic dmFtc2k6cGFzc3dvcmQ=");

     

      patch.setHeader("Content-Type", "application/json");

     

      CloseableHttpClient httpclient = HttpClients.createDefault();

     

      response = httpclient.execute(patch);

     

      HttpEntity entity = response.getEntity();

      if (entity != null) {

      long len = entity.getContentLength();

      if (len != -1 && len < 2048) {

      builder.append(EntityUtils.toString(entity));

     

      } else {

     

      return "Invalid Content Length";

      }

      }

      } catch (ClientProtocolException e) {

     

      builder.append(e.getMessage());

      builder.append(e.getStackTrace());

     

      } catch (IOException e) {

     

      builder.append(e.getMessage());

      builder.append(e.getStackTrace());

     

      } finally {

      if (response != null) {

      try {

      response.close();

      } catch (IOException e) {

      builder.append(e.getMessage());

      builder.append(e.getStackTrace());

     

      }

      }

      }

     

      return builder.toString();



  • 6.  Re: Methods supported in REST Step

    Posted Aug 24, 2016 07:28 AM

    Hi Vamsi,

     

    Java code worked. Thank you.

     

    But the earlier one to use POST method with "_HttpMethod=PATCH' as Query Parameter did not work.

    I used the below URL and I got "invalid" URL error message on using it.

    http://hostname:port/action/1236/suspend?HttpMethod=PATCH



  • 7.  Re: Methods supported in REST Step

    Posted Aug 30, 2016 06:13 AM

    Hi vamsi,

     

    The java code worked as told earlier. But we need to send request as well sometimes along with URI. Could you please let us know how to achieve this.

     

    Thanks,

    Yasaswani K



  • 8.  Re: Methods supported in REST Step

    Posted Aug 30, 2016 10:29 AM

    import java.io.IOException;

     

    import org.apache.http.HttpEntity;

    import org.apache.http.client.ClientProtocolException;

    import org.apache.http.client.methods.CloseableHttpResponse;

    import org.apache.http.client.methods.HttpPatch;

    import org.apache.http.entity.StringEntity;

    import org.apache.http.impl.client.CloseableHttpClient;

    import org.apache.http.impl.client.HttpClients;

    import org.apache.http.util.EntityUtils;

     

    StringBuilder builder = new StringBuilder();

     

    CloseableHttpResponse response = null;

     

    try {

     

    HttpPatch patch = new HttpPatch("http://localhost:6001/addBooks");

     

    patch.setHeader("Content-Type", "application/json");

     

     

    String body = "{"+

    "   \"book\": ["+

    " "+

    "      {"+

    "         \"id\":\"01\","+

    "         \"language\": \"Java\","+

    "         \"edition\": \"third\","+

    "         \"author\": \"Herbert Schildt\""+

    "      },"+

    " "+

    "      {"+

    "         \"id\":\"07\","+

    "         \"language\": \"C++\","+

    "         \"edition\": \"second\","+

    "         \"author\": \"E.Balagurusamy\""+

    "      }"+

    "   ]"+

    "}";

     

    StringEntity entity = new StringEntity(body);

     

    patch.setEntity(entity);

     

    CloseableHttpClient httpclient = HttpClients.createDefault();

     

    response = httpclient.execute(patch);

     

    HttpEntity entityResponse = response.getEntity();

    if (entityResponse != null) {

    long len = entityResponse.getContentLength();

    if (len != -1 && len < 2048) {

    builder.append(EntityUtils.toString(entityResponse));

     

    } else {

     

    return "Invalid Content Length";

    }

    }

    } catch (ClientProtocolException e) {

     

    builder.append(e.getMessage());

    builder.append(e.getStackTrace());

     

    } catch (IOException e) {

     

    builder.append(e.getMessage());

    builder.append(e.getStackTrace());

     

    } finally {

    if (response != null) {

    try {

    response.close();

    } catch (IOException e) {

    builder.append(e.getMessage());

    builder.append(e.getStackTrace());

     

    }

    }

    }

     

    return builder.toString();

     

    }



  • 9.  Re: Methods supported in REST Step

    Posted Apr 20, 2017 04:21 PM

    I changed the HttpPatch URL and the token. Everything else is the same, but I got error below:

     

    sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target[Ljava.lang.StackTraceElement;@4a72a378

     

    Is there anything else need to be changed for our environment?

     

    thanks,

    Shurong



  • 10.  Re: Methods supported in REST Step

    Posted Jun 27, 2017 12:59 PM

    Yes, that's the problem with the code. It does not support https.



  • 11.  Re: Methods supported in REST Step

    Posted Oct 05, 2017 10:10 AM

    I tried this code but I am getting this error: Also, have to set this up as BeanShell in order to not get multiple errors.

     

     

    ============================================================================
    | Error in Script
    ============================================================================
    | Step:        Execute script (JSR-223)
    ----------------------------------------------------------------------------
    | Message:     bsh.TargetError: Sourced file: inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' : Typed variable declaration : Object constructor : at Line: 17 : in file: inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' : new HttpPatch ( "http://vsdistcdap02tfs:8080/AmfamEng/AFIMirror/_apis/wit/workitems/$Test Case" )

    Target exception: java.lang.IllegalArgumentException: Illegal character in path at index 72: http://vsdistcdap02tfs:8080/AmfamEng/AFIMirror/_apis/wit/workitems/$Test Case
     in inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' at line number 17
    ----------------------------------------------------------------------------
    | Trapped Exception: bsh.TargetError: Sourced file: inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' : Typed variable declaration : Object constructor : at Line: 17 : in file: inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' : new HttpPatch ( "http://vsdistcdap02tfs:8080/AmfamEng/AFIMirror/_apis/wit/workitems/$Test Case" )

    Target exception: java.lang.IllegalArgumentException: Illegal character in path at index 72: http://vsdistcdap02tfs:8080/AmfamEng/AFIMirror/_apis/wit/workitems/$Test Case
     in inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' at line number 17
    | Trapped Message:   javax.script.ScriptException: bsh.TargetError: Sourced file: inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' : Typed variable declaration : Object constructor : at Line: 17 : in file: inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' : new HttpPatch ( "http://vsdistcdap02tfs:8080/AmfamEng/AFIMirror/_apis/wit/workitems/$Test Case" )

    Target exception: java.lang.IllegalArgumentException: Illegal character in path at index 72: http://vsdistcdap02tfs:8080/AmfamEng/AFIMirror/_apis/wit/workitems/$Test Case
     in inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' at line number 17
    ----------------------------------------------------------------------------
    STACK TRACE
    javax.script.ScriptException: bsh.TargetError: Sourced file: inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' : Typed variable declaration : Object constructor : at Line: 17 : in file: inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' : new HttpPatch ( "http://vsdistcdap02tfs:8080/AmfamEng/AFIMirror/_apis/wit/workitems/$Test Case" )

    Target exception: java.lang.IllegalArgumentException: Illegal character in path at index 72: http://vsdistcdap02tfs:8080/AmfamEng/AFIMirror/_apis/wit/workitems/$Test Case
     in inline evaluation of: ``import java.io.IOException;   import org.apache.http.HttpEntity;  import org.apa . . . '' at line number 17
     at bsh.BshScriptEngine.evalSource(BshScriptEngine.java:97)
     at bsh.BshScriptEngine.eval(BshScriptEngine.java:61)
     at javax.script.AbstractScriptEngine.eval(Unknown Source)
     at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:670)
     at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:427)
     at com.itko.lisa.test.UserScriptNode._execute(UserScriptNode.java:210)
     at com.itko.lisa.editor.UserScriptNodeEditor.execute(UserScriptNodeEditor.java:214)
     at com.itko.lisa.editor.UserScriptNodeEditor.access$000(UserScriptNodeEditor.java:49)
     at com.itko.lisa.editor.UserScriptNodeEditor$1.actionPerformed(UserScriptNodeEditor.java:73)
     at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
     at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
     at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
     at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
     at java.awt.Component.processMouseEvent(Unknown Source)
     at javax.swing.JComponent.processMouseEvent(Unknown Source)
     at java.awt.Component.processEvent(Unknown Source)
     at java.awt.Container.processEvent(Unknown Source)
     at java.awt.Component.dispatchEventImpl(Unknown Source)
     at java.awt.Container.dispatchEventImpl(Unknown Source)
     at java.awt.Component.dispatchEvent(Unknown Source)
     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
     at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
     at java.awt.Container.dispatchEventImpl(Unknown Source)
     at java.awt.Window.dispatchEventImpl(Unknown Source)
     at java.awt.Component.dispatchEvent(Unknown Source)
     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
     at java.awt.EventQueue.access$500(Unknown Source)
     at java.awt.EventQueue$3.run(Unknown Source)
     at java.awt.EventQueue$3.run(Unknown Source)
     at java.security.AccessController.doPrivileged(Native Method)
     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
     at java.awt.EventQueue$4.run(Unknown Source)
     at java.awt.EventQueue$4.run(Unknown Source)
     at java.security.AccessController.doPrivileged(Native Method)
     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
     at java.awt.EventQueue.dispatchEvent(Unknown Source)
     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
     at java.awt.EventDispatchThread.run(Unknown Source)
    Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 72: http://vsdistcdap02tfs:8080/AmfamEng/AFIMirror/_apis/wit/workitems/$Test Case
     at java.net.URI.create(Unknown Source)
     at org.apache.http.client.methods.HttpPatch.<init>(HttpPatch.java:67)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
     at java.lang.reflect.Constructor.newInstance(Unknown Source)
     at bsh.Reflect.constructObject(Reflect.java:575)
     at bsh.BSHAllocationExpression.constructObject(BSHAllocationExpression.java:125)
     at bsh.BSHAllocationExpression.objectAllocation(BSHAllocationExpression.java:114)
     at bsh.BSHAllocationExpression.eval(BSHAllocationExpression.java:62)
     at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102)
     at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)
     at bsh.BSHVariableDeclarator.eval(BSHVariableDeclarator.java:86)
     at bsh.BSHTypedVariableDeclaration.eval(BSHTypedVariableDeclaration.java:84)
     at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
     at bsh.BSHBlock.eval(BSHBlock.java:80)
     at bsh.BSHBlock.eval(BSHBlock.java:46)
     at bsh.BSHTryStatement.eval(BSHTryStatement.java:88)
     at bsh.Interpreter.eval(Interpreter.java:664)
     at bsh.Interpreter.eval(Interpreter.java:758)
     at bsh.Interpreter.eval(Interpreter.java:747)
     at bsh.BshScriptEngine.evalSource(BshScriptEngine.java:89)
     ... 44 more
    Caused by: java.net.URISyntaxException: Illegal character in path at index 72: http://vsdistcdap02tfs:8080/AmfamEng/AFIMirror/_apis/wit/workitems/$Test Case
     at java.net.URI$Parser.fail(Unknown Source)
     at java.net.URI$Parser.checkChars(Unknown Source)
     at java.net.URI$Parser.parseHierarchical(Unknown Source)
     at java.net.URI$Parser.parse(Unknown Source)
     at java.net.URI.<init>(Unknown Source)
     ... 66 more
    ============================================================================



  • 12.  Re: Methods supported in REST Step

    Posted Oct 05, 2017 11:35 AM

    Hi Jeff, 

     

    I tried the following code with your URL  and it is working for me on DevTest 10.1

     

    You need the change the body with your request body and test it, if you can provide me request/response pairs I can change the body and send it to you. 

     

     

    import java.io.IOException;

    import org.apache.http.HttpEntity;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPatch;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;

    StringBuilder builder = new StringBuilder();

    CloseableHttpResponse response = null;

    try {

    HttpPatch patch = new HttpPatch("http://localhost:8980/AmfamEng/AFIMirror/_apis/wit/workitems/$Test");

    patch.setHeader("Content-Type", "application/json");


    String body = "{"+
    " \"book\": ["+
    " "+
    " {"+
    " \"id\":\"01\","+
    " \"language\": \"Java\","+
    " \"edition\": \"third\","+
    " \"author\": \"Herbert Schildt\""+
    " },"+
    " "+
    " {"+
    " \"id\":\"07\","+
    " \"language\": \"C++\","+
    " \"edition\": \"second\","+
    " \"author\": \"E.Balagurusamy\""+
    " }"+
    " ]"+
    "}";

    StringEntity entity = new StringEntity(body);

    patch.setEntity(entity);

    CloseableHttpClient httpclient = HttpClients.createDefault();

    response = httpclient.execute(patch);

    HttpEntity entityResponse = response.getEntity();
    if (entityResponse != null) {
    long len = entityResponse.getContentLength();
    if (len != -1 && len < 2048) {
    builder.append(EntityUtils.toString(entityResponse));

    } else {

    return "Invalid Content Length";
    }
    }
    } catch (ClientProtocolException e) {

    builder.append(e.getMessage());
    builder.append(e.getStackTrace());

    } catch (IOException e) {

    builder.append(e.getMessage());
    builder.append(e.getStackTrace());

    } finally {
    if (response != null) {
    try {
    response.close();
    } catch (IOException e) {
    builder.append(e.getMessage());
    builder.append(e.getStackTrace());

    }
    }
    }

    return builder.toString();

    }



  • 13.  Re: Methods supported in REST Step

    Posted Oct 05, 2017 12:17 PM

    Hi Vamsi,

     

    Thanks for the quick response. However, I found another solution that is now letting me pass PATCH using POST in the REST step in DevTest.

     

    Jeff De Meyer

    Software Engineering Unit

    Software Engineer Specialist<javascript:getPeopleByTitle('Software%20Eng%20Tools%20Specialist');>



  • 14.  Re: Methods supported in REST Step

    Posted Oct 06, 2017 02:38 PM

    Cool