Calling api from script


#1

Hi,
I am getting “executed command returned with error code 1”

here are the few get api I tried

var userObject = platform.api.get(‘system/user/2?related=user_to_app_to_role_by_user_id’);
var apt = platform.api.get( “mysql/_table/appointment” );
platform.api.get( “http://www.google.com” );

any idea what am i doing wrong


#2

Hey there @Vinod_Hum,

We’ve got some great resources you should check out:

Between these two resources, you should be able to get the full picture. :slight_smile:

Thanks,
@AlexBowen


#3

Thanx Alex,
I checked all the resource and I am able to call rest api from service script but rest api is not working from pre and post script. I don’t know if anybody else has reported this issue.


#4

What language are you using for scripting?


#5

I am using node js.
when I use same code with service script it works but not with pre/post script


#6

Thanks for the response.

Here’s a few more resources that may help you:

When we see this, it’s usually because there was an error during the script writing process. Have you pasted your script in a code editor to check?

Our built in code editor has BASIC syntax checking. But that would only alert you if you don’t close the line or try use a deprecated function, for example. Therefore, a more robust IDE would be ideal for troubleshooting the things that are not DreamFactory specific.

Thanks,
@AlexBowen


#7

Hi all, I’m experiencing similar problem, whenever I call “platform.api.get” DF always returned HTTP 500.
I added below code to one of my API (using method PATCH) pre-processing script:


if (event.request.payload.myfield) {
  var result = platform.api.get("/user/session");
  console.log(result);
}

and DF log shows

[2016-08-11 12:31:00] production.DEBUG: Executed script: node -e ’

_wrapperResult = (function() {
… bunch of js code …
console.log(JSON.stringify(_wrapperResult));’
[2016-08-11 12:31:01] production.INFO: [RESPONSE] {“Status Code”:500,“Content-Type”:“application/json”}

which doesn’t really ring a bell, end up in frustration, I copy “node -e '…(_wrapperResult));” and run in shell, and this is the error returned

                callback(body, response);
                ^

TypeError: undefined is not a function
at IncomingMessage. (eval:106:21)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:944:16
at process._tickCallback (node.js:448:13)

Not sure if it’s missing nodejs packages, I went to node_module directory and installed callback and bunch of other packages, still getting HTTP 50 whenever platform.api.get is called.

Here’s my setup

  • CentOS 7 x64
  • Apache 2.4.6
  • DreamFactory 2.2.1, installed from github
  • service access: service “all”, component “*”, access all 5, requester “api, script” (I know it’s bad, will change accordingly after I solve this weird question)

If any of you experienced/solved similar problem, please do share the solution, thanks in advance.
(Sorry to hijack your thread, I tried to create a new topic but I kept getting 500 Internal server error.)


#8

Looks like the correct syntax to call platform.api.* function in nodeJS is

platform.api.get('/db/_table/todo', '', function(body, response){
    event.setResponse(JSON.parse(body), response.statusCode, 'applicaton/json');
});

according to this URL http://wiki.dreamfactory.com/DreamFactory/Tutorials/Node_API_example
is there any reason why the documentation for server-side scripting is inconsistent?


#9

I’m glad to see that you figured it out, @lewis! Thanks for posting your answer.

Would you be able to elaborate on this comment? We always appreciate the opportunity to make our documentation better. The more specific you can get, the better. :slight_smile:

Thanks,
@AlexBowen


#10

@AlexBowen
Only in the URL http://wiki.dreamfactory.com/DreamFactory/Tutorials/Node_API_example uses

platform.api.get(’/db/_table/todo’ , ‘’ , function(body, response) {

while the rest of documentation repeatedly show something like below

var result = platform.api.get(’/db/_table/todo’);
… then do something with result

That’s somehow confusing for newbie like me, I kept trying to debug why the latter doesn’t work until I found that URL and change the syntax to that.

Btw, we’re having problem with pre-process script, a little help here ? thanks.


#11

Thanks @lewis, I’m looking into it. :slight_smile:


#12

ok her is my finding,
after installing instance to cloud EC2 it worked don’t know what was the glitch with local install.
thanx