Calling REST methods from Script Component

Posted by Timothy Williamson on 06-Jun-2017 07:09

Hello,

I'm working on a way to drag/drop files into the browser and create records/upload the binary content at the same time. It *almost* works (I think, lol)...but I'm running in to problems when posting to "setBinaryData".

Here is the line where I generate the sessionId:

"sessionId={!#SESSION_ID}@{!#CURR_CUSTM.id}"

...I keep getting "Session expired or invalid login credentials" back in the response.

Is there a way to generate a valid sessionId using tokens?

Having to call "login" kinda defeats the purpose so, if at all possible, using the tokens (or some other way I don't know about :)) would be preferred here.

Posted by Shiva Duriseati on 08-Jun-2017 02:01

Hi Tim,

 

The "sessionid"  token which is displayed under token helpers is not meant for using in REST API calls. So technically you cannot use REST API calls unless providing login credentials again which is not recommended.

If you are on Private Cloud one way you could do without using REST API is to create a dummy textarea field and update this with b64 value. And using this value create image with server-side API "setBinaryFieldValue". by calling rbf_runTrigger() from client side.

 

Following are the steps which I've performed.(From previous comments I am assuming you knew record id  ) 

1)Created a dummy textarea field "test".

2)Updated this field with Client-Side API rbf_updateRecord().

(Note: Since b64 is huge string it should be embedded inside backticks also increase the "maxHttpHeaderSize" in server.xml)

3)Create a object script trigger without enabling "Trigger Timing" options.

and use following code inside trigger section.

var encodedValue=rbv_api.getFieldValue("SimpleObject", {!id}, "test");

rbv_api.setBinaryFieldValue("SimpleObject", "{!id}", "image", encodedValue, "image/jpg", "sample.jpg");

 

4)Now from the script component use rbf_runTrigger() to run this trigger

 

rbf_runTrigger("SimpleObject","14024" , "-5pKaZoaQ0mQUG361pMlRA", false);

 

Please note still there must be some better way to do this.

 

Regards,

Shiva

All Replies

Posted by Timothy Williamson on 06-Jun-2017 12:03

This question is based on the fact that we can't upload binary content via the AJAX API.

I picked that up from reading through a few other threads but could be wrong. :)

Posted by marian.edu on 06-Jun-2017 13:52
unless you base64 encode it on the client and send it in a plain json together with the rest of your fields, check out stackoverflow for ways to read base64 encoded data out of a file input…

Marian Edu

Acorn IT 
+40 740 036 212

Posted by Timothy Williamson on 06-Jun-2017 15:02

I've got the encoding part all squared away. Here's an example of the custom create/upload I've working on. (Just can't figure out how to get a valid session Id for the REST API while logged in as a user.)

BTW, I just tried the suggestion; it makes the request uri too long. I'm curious, has that worked in the past?

Posted by Timothy Williamson on 06-Jun-2017 15:22

Just a side note, that demo is rigged. :) It only works because I hard code a session ID that I grabbed using an API tester. That's the part I'm trying to resolve.

Posted by Shiva Duriseati on 08-Jun-2017 02:01

Hi Tim,

 

The "sessionid"  token which is displayed under token helpers is not meant for using in REST API calls. So technically you cannot use REST API calls unless providing login credentials again which is not recommended.

If you are on Private Cloud one way you could do without using REST API is to create a dummy textarea field and update this with b64 value. And using this value create image with server-side API "setBinaryFieldValue". by calling rbf_runTrigger() from client side.

 

Following are the steps which I've performed.(From previous comments I am assuming you knew record id  ) 

1)Created a dummy textarea field "test".

2)Updated this field with Client-Side API rbf_updateRecord().

(Note: Since b64 is huge string it should be embedded inside backticks also increase the "maxHttpHeaderSize" in server.xml)

3)Create a object script trigger without enabling "Trigger Timing" options.

and use following code inside trigger section.

var encodedValue=rbv_api.getFieldValue("SimpleObject", {!id}, "test");

rbv_api.setBinaryFieldValue("SimpleObject", "{!id}", "image", encodedValue, "image/jpg", "sample.jpg");

 

4)Now from the script component use rbf_runTrigger() to run this trigger

 

rbf_runTrigger("SimpleObject","14024" , "-5pKaZoaQ0mQUG361pMlRA", false);

 

Please note still there must be some better way to do this.

 

Regards,

Shiva

This thread is closed