Service Virtualization

Expand all | Collapse all

REST call with method "PATCH"

  • 1.  REST call with method "PATCH"

    Posted Aug 25, 2016 05:03 PM

    All,

    There is a requirement in my project to make a REST call with Method "PATCH". But I don't see that in the version that i'm using. I'm only seeing four methods now "GET","POST","DELETE","PUT".

    Is there any way I can do this? We are using LISA V 7.5.1



  • 2.  Re: REST call with method "PATCH"

    Broadcom Employee
    Posted Aug 25, 2016 05:09 PM

    Refer to this Communities Post:

    Methods supported in REST Step 



  • 3.  Re: REST call with method "PATCH"

    Posted Aug 25, 2016 05:16 PM

    Hi Pradeep, 

     

       If you want to use the code from Methods supported in REST Step  you also need to Apache http-client jars (httpclient-4.3.6.jar) , I don't think , Lisa 7.5.2 will come with these jars. 

     

    download like for jar httpclient-4.3.6.jar

     

    Thanks,

    Vamsi 



  • 4.  Re: REST call with method "PATCH"

    Posted Aug 26, 2016 09:28 AM

    Hi Vamsi,

    Thanks for the reply.

     

    I took the java code from the link that you provided and also got the jar you suggested. The only doubt is how do I pass the request body and I didn't find anything being passed in the java code. I may be wrong. Please help.



  • 5.  Re: REST call with method "PATCH"

    Posted Aug 26, 2016 10:14 AM

    Pradeep, 

     

    Below is the sample code to pass the request body, If you need more help on http-client API , please go through the apache API documentation (Apache HttpClient 4.3.6 API).

     

     

    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;

     

    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();

     

    }

     

     

     

     

    Thanks,

    Vamsi



  • 6.  Re: REST call with method "PATCH"

    Posted Sep 14, 2016 01:27 PM

    Vamsi,

    Thanks for your help.


    1. I have written the java code for patch as suggested by you copied the following jar files httpclient-4.3.6.jar and httpcore-4.4.4.jar to {LISA_HOME}\lib).
    2. I had added environment variable LISA_PRE_CLASSPATH to override the LISA default jars and the reason I did that is because there was an exception in LISA like--> "Target exception: java.lang.NoSuchFieldError: INSTANCE".
    3. That issue got resolved once I added that LISA_PRE_CLASSPATH but the issue now is that standard REST STEP (POST method) isn't working and getting the below error
    4, Could you please help us on this. 


    ============================================================================ | HTTP ============================================================================ | Step: Fuel-Site-Details API - MPS ---------------------------------------------------------------------------- | Message: java.lang.NoSuchMethodError: org.apache.http.conn.scheme.SchemeRegistry.removeOverrideHttpsScheme()V ---------------------------------------------------------------------------- | Trapped Exception: org.apache.http.conn.scheme.SchemeRegistry.removeOverrideHttpsScheme()V | Trapped Message: java.lang.NoSuchMethodError: org.apache.http.conn.scheme.SchemeRegistry.removeOverrideHttpsScheme()V ---------------------------------------------------------------------------- STACK TRACE java.lang.NoSuchMethodError: org.apache.http.conn.scheme.SchemeRegistry.removeOverrideHttpsScheme()V at com.itko.lisa.test.CommTrans.postSend(CommTrans.java:812) at com.itko.lisa.test.CommTrans.send(CommTrans.java:797) at com.itko.lisa.test.CommTrans.sendPOST(CommTrans.java:773) at com.itko.lisa.ws.rest.RESTNode.doSend(RESTNode.java:214) at com.itko.lisa.ws.rest.RESTNode.doWebTrans(RESTNode.java:167) at com.itko.lisa.ws.rest.RESTNodeBase.execute(RESTNodeBase.java:363) at com.itko.lisa.test.TestNode.executeNode(TestNode.java:993) at com.itko.lisa.test.TestCase.execute(TestCase.java:1160) at com.itko.lisa.test.TestCase.execute(TestCase.java:1075) at com.itko.lisa.test.TestCase.executeNextNode(TestCase.java:1060) at com.itko.lisa.editor.WalkThruPanel.prepAndExecNode(WalkThruPanel.java:1047) at com.itko.lisa.editor.WalkThruPanel.access$900(WalkThruPanel.java:68) at com.itko.lisa.editor.WalkThruPanel$10.doCallback(WalkThruPanel.java:960) at com.itko.util.swing.panels.ProcessingDialog$2.run(ProcessingDialog.java:194) at java.lang.Thread.run(Unknown Source) ============================================================================



  • 7.  Re: REST call with method "PATCH"

    Posted Sep 15, 2016 10:53 AM

    Hi Pradeep, 

     

      I think the only jar you need to run the REST PATCH code is httpclient-4.3.6.jar, just copy this jar to hot deploy directory. 

     

    What are jars added to LISA_PRE_CLASSPATH ? 

     

    I will also test the code on 7.5.2 and get back to you by today evening. 

     

     

    Thanks, 

    Vamsi 

     



  • 8.  Re: REST call with method "PATCH"

    Posted Sep 15, 2016 04:54 PM

    Vamsi,

    I did try today. Removed httpcore-4.4.4.jarand made sure I had only httpclient-4.3.6.jar in the lib folder. Still no luck.

    I even tried with placing the jar in hot deploy directory but still no luck.

     

    To answer your question regarding LISA_PRE_CLASSPATH, I had both jars. 

    Let me know what you find out. Thanks for your help!!!



  • 9.  Re: REST call with method "PATCH"
    Best Answer

    Posted Sep 16, 2016 06:04 AM

    Look like HttpClient jar is conflicting with one of the existing jars , I have tried with another API and it works fine , I verified on Lisa 7.5.2

     

    Download  okhttp-3.4.1.jar, okio-1.9.0.jar from this link OkHttp ,  okio  and copy them to LISA hotDeploy directory. 

     

    Below is the javascript code  you have to use to send the PATCH request

     

    import okhttp3.MediaType;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import okhttp3.Response;

    OkHttpClient client = new OkHttpClient();

    MediaType mediaType = MediaType.parse("application/json");

    String url = "http://devtest.bizcapps.com:8080/books";

    String requestBody = "{\"language\" : \"java\"}";

    RequestBody body = RequestBody.create(mediaType, requestBody);

    Request request = new Request.Builder()
    .url(url)
    .patch(body)
    .addHeader("content-type", "application/json")
    .addHeader("cache-control", "no-cache")
    .build();

    Response response = client.newCall(request).execute();

    return response.body().string();



  • 10.  Re: REST call with method "PATCH"

    Posted Sep 22, 2016 02:57 PM

    Thanks Vamsi. It's working now. Appreciate your help!!!



  • 11.  Re: REST call with method "PATCH"

    Posted Jun 27, 2017 12:19 PM

    Hi Vamsi, I am trying to using this code for Patch method for rest call over Https but I am getting SSL error. However, using inbuild  Devtest Rest step all the methods are working fine and I am not getting any SSL issue. I have not installed/configured any specific SSL certs.

    Your help will be much appreciated. I am pasting the complete error below:

    [SSL Handshake Summary] javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

     

    Thanks



  • 12.  Re: REST call with method "PATCH"

    Posted Jul 11, 2017 09:05 AM

    Hi Vamsi,

    When I am trying the above java script I am getting below error

    | Error in Script
    ============================================================================
    | Step:        Java Script Step
    ----------------------------------------------------------------------------
    | Message:     Sourced file: inline evaluation of: ``import okhttp3.*;    OkHttpClient client = new OkHttpClient();    MediaType medi . . . '' : Typed variable declaration : Class: OkHttpClient not found in namespace : at Line: 3 : in file: inline evaluation of: ``import okhttp3.*;    OkHttpClient client = new OkHttpClient();    MediaType medi . . . '' : OkHttpClient

     

     Could you please help