This is how I tried and it is working as expected.
<core:set var="apiURL" value="" />
<core:set var="username" value="" />
<core:set var="password" value="" />
<core:new className="java.net.URL" var="remoteURL" >
<core:arg type="java.lang.String" value="${apiURL}" />
</core:new>
<gel:log>${apiURL}</gel:log>
<core:invokeStatic var="base64" className="com.niku.union.utility.Base64" method="encode">
<core:arg type="java.lang.String" value="${username}:${password}" />
</core:invokeStatic>
Original Message:
Sent: 09-03-2020 10:49 AM
From: Martin Snizek
Subject: GEL Script - call rest api with x-www-form-urlencoded body
Hello,
yes, but I have anonymized the tenantId - when I try to call it I replace "myTenantId" with real and in Postman working value.
Regards,
Martin
Original Message:
Sent: 09-03-2020 10:44 AM
From: Arunachalam Angusamy
Subject: GEL Script - call rest api with x-www-form-urlencoded body
Hello Martin,
Is this the REST API URL that you are trying to call
https://accounts.accesscontrol.windows.net/myTenantId/tokens/OAuth/2
Original Message:
Sent: 09-02-2020 11:22 AM
From: Martin Snizek
Subject: GEL Script - call rest api with x-www-form-urlencoded body
The script as a file, because in my initial message it looks terrible.
Martin
Original Message:
Sent: 09-02-2020 10:17 AM
From: Martin Snizek
Subject: GEL Script - call rest api with x-www-form-urlencoded body
Hi all,
I have an issue with calling rest API with x-www-form-urlencoded body. It should be used for authorization with Sharepoint, but I am not able to put the body to the request.
Below you can see my script:
<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"> <core:catch var="SuperException"> <gel:log>Started 2</gel:log> <core:set var="loginURLstring">https://accounts.accesscontrol.windows.net/myTenantId/tokens/OAuth/2</core:set> <core:new className="java.net.URL" var="loginURL" > <core:arg type="java.lang.String" value="${loginURLstring}" /> </core:new> <gel:log>${loginURL}</gel:log> <core:set var="odeslat_data" value="grant_type=client_credentials&client_id=myClientId&client_secret=myClientSecret=&resource=myResourceValue" /> <gel:log>${odeslat_data}</gel:log> <core:set var="LoginConnection" value="${loginURL.openConnection()}"/> <core:expr value="${LoginConnection.setDoOutput(true)}" /> <core:expr value="${LoginConnection.setDoInput(true)}" /> <core:expr value='${LoginConnection.setRequestMethod("POST")}'/> <core:expr value='${LoginConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded")}'/> <gel:log>${LoginConnection}</gel:log> <gel:log>Connection ready</gel:log> <core:set var="void" value="${LoginConnection.connect()}"/> <gel:log>Connected</gel:log> <core:new className="java.io.OutputStreamWriter" var="wr1"> <core:arg type="java.io.OutputStream" value="${LoginConnection.getOutputStream()}"/> </core:new> <core:set var="void" value="${wr1.write(odeslat_data)}"/> <core:set var="void" value="${wr1.flush()}"/> <core:set var="void" value="${wr1.close()}"/> <gel:log>Body sent</gel:log> <core:set var="loginResponseCode" value="${LoginConnection.getHeaderField(0)}" /> <core:set var="loginResponseMessage" value="${LoginConnection.getResponseMessage()}" /> <gel:log>HTTP response: ${loginResponseCode}</gel:log> <gel:log>Response message: ${loginResponseMessage}</gel:log> </core:catch> <core:if test="${SuperException!=null}"> <gel:log>Super Exception - ${SuperException}</gel:log> </core:if></gel:script>
From postman, my tests finish well but from the script I always get:HTTP response: HTTP/1.1 401 Unauthorized
I suppose that there is a problem with providing the body to the remote webservice, but I have no idea how to do that. Does anyone has any experience or example how to do this?
Thank you all in advance,
Martin