Clarity

 View Only
  • 1.  Using PATCH Method REST API with gelscript

    Posted Dec 10, 2020 09:13 AM
    Edited by Mohammed TAHRI-JOUTEY Dec 10, 2020 09:15 AM
    Dear Community, 

    For my customer, we use Custom investments object, because we can't create or update instances with XOG, I am trying to test the REST API with Gelscript, I am able to read and create instance using REST API with Gelscript, but I have some dificulty to send a PATCH request to update some attributes, 

    I am getting the error 405 for all my tests 

    Below my script, if you can help

    <gel:script xmlns="http://www.w3.org/2001/XMLSchema"

    xmlns:core="jelly:core"

    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"

    xmlns:sql="jelly:sql" xmlns:xog="http://www.niku.com/xog"

    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:util="jelly:util">


    <gel:parameter default="true" var="DebugClarity"/>
    <gel:parameter default="1" var="cl_CapabilityID"/>

    <core:catch var="ScriptException">

    <core:invokeStatic className="java.lang.System" method="currentTimeMillis" var="milStart00"/>

    <core:if test="${DebugClarity}"><gel:log/><gel:log>Basic Auth Encoded 64: ${basicAuth}</gel:log></core:if>

    <!-- *** Setting REST URL for POSTing Project *** -->

    <core:set var="httpRESTURL">${ClarityRestUrl}/custXCapabilitiess/${cl_CapabilityID}</core:set>

    <core:if test="${DebugClarity}"><gel:log/><gel:log>httpRESTURL: ${httpRESTURL}</gel:log></core:if>

    <core:new className="java.net.URL" var="remoteURL" ><core:arg type="java.lang.String" value="${httpRESTURL}" /></core:new>

    <!-- *** Setting Connection Details *** -->

    <core:set var="ClarityConnection" value="${remoteURL.openConnection()}"/>

    <core:expr value="${ClarityConnection.setDoOutput(true)}" />

    <core:expr value="${ClarityConnection.setDoInput(true)}" />

    <core:expr value="${ClarityConnection.setConnectTimeout(180000)}" />

    <core:expr value="${ClarityConnection.setReadTimeout(180000)}" />

    <core:expr value='${ClarityConnection.setRequestMethod("PATCH")}'/>

    <core:expr value='${ClarityConnection.setRequestProperty("Content-type", "application/json")}'/>

    <core:expr value='${ClarityConnection.setRequestProperty("Accept", "application/json")}'/>

    <core:expr value='${ClarityConnection.setRequestProperty("cache-control", "no-cache")}'/>

    <core:expr value='${ClarityConnection.setRequestProperty("Authorization", basicAuth)}'/>

    <!-- *** Sending Request for psodting details from Connection *** -->

    <core:set var="void" value="${ClarityConnection.connect()}"/>




    <!-- *** Setting Project creation variable *** -->

    <core:set var="UpdateCapability" escapeText="false">

    <![CDATA[{"departmentCode": "031M"}]]>

    </core:set>
    <gel:log>[Before : ${ClarityConnection.getOutputStream()}]</gel:log>
    <core:new className="java.io.OutputStreamWriter" var="wr1">

    <core:arg type="java.io.OutputStream" value="${ClarityConnection.getOutputStream()}"/>

    </core:new>

    <gel:log>[After : ${ClarityConnection.getOutputStream()}]</gel:log>

    <core:set var="void" value="${wr1.write(UpdateCapability)}"/>

    <core:set var="void" value="${wr1.flush()}"/>

    <core:set var="void" value="${wr1.close()}"/>

    <!-- *** Fetching Responce from Connection *** -->

    <core:set var="CL_isAuthenticated" value="${ClarityConnection.getHeaderField(0)}" />

    <core:set var="response_msg" value="${ClarityConnection.getResponseMessage()}" />

    <core:if test="${DebugClarity}">

    <gel:log>isAuthenticated: ${CL_isAuthenticated}</gel:log>

    <gel:log>response_msg: ${response_msg}</gel:log>

    </core:if>

    <core:if test="${CL_isAuthenticated=='HTTP/1.1 200'}">

    <gel:log><![CDATA[************ Update OK *********]]></gel:log>

    </core:if>

    <core:if test="${CL_isAuthenticated!='HTTP/1.1 200'}">

    <gel:log><![CDATA[************ Updaye KO *********]]></gel:log>

    </core:if>

    <core:invokeStatic className="java.lang.System" method="currentTimeMillis" var="milEnd02"/>

    <core:set var="elapsedTime" value="${(milEnd02 - milStart00)}"/>

    <core:if test="${DebugTimings}"><gel:log><![CDATA[Time taken to extract Information= ${elapsedTime/1000} sec ]]></gel:log></core:if>

    </core:catch>

    <core:if test="${ScriptException!=null}"><gel:log>ScriptException - ${ScriptException}</gel:log></core:if>

    <core:invokeStatic className="java.lang.System" method="currentTimeMillis" var="milEnd00"/>

    <core:set var="elapsedTime" value="${(milEnd00 - milStart00)}"/>

    <core:if test="${DebugTimings}"><gel:log><![CDATA[Time taken by script= ${elapsedTime/1000} sec ]]></gel:log></core:if>

    </gel:script>


    Regards


  • 2.  RE: Using PATCH Method REST API with gelscript
    Best Answer

    Posted Dec 14, 2020 04:03 AM
    Hello, 

    I fixed my issue, PATCH doesn't work properly, I used the PUT with the header "x-api-force-patch=true"  and it works, 

    Regards


  • 3.  RE: Using PATCH Method REST API with gelscript

    Posted Mar 22, 2022 01:59 PM
    Hello!
    I'm trying to do the same thing (send a "PATCH" rest request) but the "x-api-force-patch=true" header is not working.
    It returns an error like this (405 Method Not Allowed):
    {
        "resourceId"null,
        "httpStatus""405",
        "errorMessage""API-1018 : Could not process the request. Refer to error code for more details.",
        "errorCode""405"
    }

    Do you have any other tips?


    ------------------------------
    [JobTitle]
    [CompanyName]
    [State]

    Bruno Pugliese
    ------------------------------



  • 4.  RE: Using PATCH Method REST API with gelscript

    Posted Mar 27, 2022 08:06 AM
    Edited by Samad AbdulAleem Mar 27, 2022 10:57 AM
    This worked for me as well

    <core:expr value='${ClarityConnection.setRequestProperty("x-api-force-patch", "true")}'/>
    <core:expr value='${ClarityConnection.setRequestMethod("PUT")}'/>

    Regards,
    Samad Abdul Aleem


  • 5.  RE: Using PATCH Method REST API with gelscript

    Posted Mar 28, 2022 10:07 AM
    Edited by Bruno Pugliese Mar 28, 2022 10:07 AM
    It did not work for me at all.

    But I did find an official example in the knowledge base using different java classes that accept a PATCH message. this one works for me.
    https://knowledge.broadcom.com/external/article?articleId=218785

    <gel:script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:wss="http://www.boomi.com/connector/wss"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xog="http://www.niku.com/xog"
    xmlns:x="jelly:org.apache.commons.jelly.tags.xml.XMLTagLibrary"
    xmlns:util="jelly:util" xmlns:sql="jelly:sql"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:jelly="jelly:core"
    xmlns:nikuq="http://www.niku.com/xog/Query"
    xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    xmlns:file="jelly:com.niku.union.gel.FileTagLibrary"
    xmlns:core="jelly:core">

    <!--***************************************Get the session from the system Start ***********************************-->
    <gel:parameter var="gel_host_user" default="admin"/>
    <gel:parameter var="gel_host_pass" default="xxxxx"/>
    <gel:setDataSource dbId="niku"/>
    <!--Set parameter here End -->
    <!--***************************************Get the session from the system End ***********************************-->



    <!-- Generate SessionID -->
    <core:new className="com.niku.union.security.DefaultSecurityIdentifier" var="secId" />
    <core:invokeStatic var="userSessionCtrl" className="com.niku.union.security.UserSessionControllerFactory" method="getInstance" />
    <core:set var="secId" value="${userSessionCtrl.init(gel_host_user, secId)}"/>
    <core:set var="sessionID" value="${secId.getSessionId()}"/>

    <core:choose>
    <core:when test="${sessionID == null}">
    <gel:log level="ERROR">Cannot login to Clarity XOG. Check username.</gel:log>
    </core:when>
    <core:otherwise>
    <gel:log>SessionID: ${sessionID}</gel:log>
    </core:otherwise>
    </core:choose>



    <!--core:invoke method="setHeader" on="${patch}"> <core:arg type="java.lang.String" value="Cookie"/>
    <core:arg type="java.lang.String" value="${v_Cookie}"/> </core:invoke-->



    <core:new var="patch" className="org.apache.http.client.methods.HttpPatch">
    <core:arg value="http://clarity/ppm/rest/v1/projects/5001083/tasks/5001170" type="java.lang.String"/>
    </core:new>
    <core:invoke on="${patch}" method="setHeader">
    <core:arg value="Authorization" type="java.lang.String"/>
    <core:arg value="Basic YWRtaW46YWRtaW4=" type="java.lang.String"/>
    </core:invoke>

    <core:invoke on="${patch}" method="setHeader">
    <core:arg value="Content-Type" type="java.lang.String"/>
    <core:arg value="application/json" type="java.lang.String"/>
    </core:invoke>

    <core:set var="createProject" escapeText="false">{"percentComplete": "1", "name": "Initiation Phase2" }</core:set>
    <!-- <core:set var="createProject" escapeText="false">{"code": "${row.CODE}", "name": "${row.NAME}", "description":"${row.DESCRIPTION}" }</core:set> -->

    <core:new var="entity" className="org.apache.http.entity.StringEntity">
    <core:arg value="${createProject}" type="java.lang.String"/>
    </core:new>
    <gel:log>entity: ${entity}</gel:log>

    <core:invoke on="${patch}" method="setEntity">
    <core:arg value="${entity}" type="org.apache.http.entity.StringEntity"/>
    </core:invoke>
    <gel:log>patch: ${patch}</gel:log>

    <core:invokeStatic var="httpclient" className="org.apache.http.impl.client.HttpClients" method="createDefault"/>
    <core:invoke var="response" on="${httpclient}" method="execute">
    <core:arg value="${patch}" type="org.apache.http.client.methods.HttpPatch"/>
    </core:invoke>
    <gel:log>response: ${response}</gel:log>

    <core:invoke var="entityResponse" on="${response}" method="getEntity"/>
    <core:invoke var="ResponseStatus" on="${response}" method="getStatusLine"/>
    <gel:log>ResponseStatus getProtocolVersion: ${ResponseStatus.getProtocolVersion()}</gel:log>
    <gel:log>ResponseStatus getReasonPhrase: ${ResponseStatus.getReasonPhrase()}</gel:log>
    <gel:log>ResponseStatus getStatusCode: ${ResponseStatus.getStatusCode()}</gel:log>
    <gel:log>entityResponse: ${entityResponse}</gel:log>
    <core:set var="void" value="${response.close()}"/>

    <gel:log>exception::${exception}</gel:log>
    </gel:script>


    ------------------------------
    [JobTitle]
    [CompanyName]
    [State]

    Bruno Pugliese
    ------------------------------