sdanigo
Posts: 0
Joined: Sat Jun 06, 2015 8:50 pm

Calling an Api Express from server code

Hi,
I'd like to call an Api Express made connexion to a mySql database from a server side javascript code.
What is the correct syntax ?
My goal is to retreive a set of data from an external database and proceed that set in a server code script.
Thanks

Sylvain

Galyna Abramovych
Site Admin
Posts: 84
Joined: Tue Mar 22, 2016 6:03 pm

Calling an Api Express from server code

Hello Sylvian,

Thank you for contacting Appery.io support!

Please have a look at this tutorial: https://devcenter.appery.io/tutorials...

Mind, that you can't call API Express service as Appery.io DB collection. Server code won't find that collection.
Please call API Express service in the server code as a simple XMLHttpRequest: https://devcenter.appery.io/documenta...

Also, you have to make available our application servers DNS: https://devcenter.appery.io/documenta...

Andrew6784445
Posts: 1
Joined: Fri Apr 04, 2014 2:08 pm

Calling an Api Express from server code

Galyna,

I have created an API Express service that connects to MySQL. The service works when tested in API Express AND works directly in my app.

I would like the service to run as Server Code but it does not work. No errors occur. Could you please elaborate on "making our application servers DNS", and any other specifics for calling API Express services from Server Code?

Also, your links are not pointing to anything specific, could you please revise.

Thanks so much,

Andrew

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Calling an Api Express from server code

Hi Andrew -

[quote:]
I would like the service to run as Server Code but it does not work.
[/quote]

Please elaborate what do you mean here?

Andrew6784445
Posts: 1
Joined: Fri Apr 04, 2014 2:08 pm

Calling an Api Express from server code

No Errors. The Service is an Update Query.

Here is the Server Code call:

XHR.send("PUT","https://api.appery.io/rest/1/apiexpre...", {
"headers": {
"apiKey":"xxxxx",
"pUserID": "1",
"pPasscode" : "abcdef",
}
});

The API Express Query is: (BTW-this works when testing in API Express and when connecting API Express Service directly to my app):

UPDATE tblUsers
SET ResetCode = :pPasscode
WHERE UserID = :pUserID

In short, I am attempting to use the SendGrid example for Password Resets using a MySQL Backend in API Express.

Thanks for your help,

Andrew

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Calling an Api Express from server code

Hello Andrew,

Please check XHR2 method of the Server Code for that issue: https://docs.appery.io/reference#serv...

Andrew6784445
Posts: 1
Joined: Fri Apr 04, 2014 2:08 pm

Calling an Api Express from server code

I changed the Server Code to the following but it still doesn't update the record:

var XHRResponse = XHR2.send("PUT","https://api.appery.io/rest/1/apiexpre...", {
"headers": {
"X-Appery-Api-Express-Api-Key" : "xxxxx",
"pUserID": "1",
"pPasscode" : "abcdef"
}

Code: Select all

 }); 

If I test the Service from My APP it works using these same Header parameters. Is there something else I am missing when calling from Server Code? No error occurs, it just doesn't do the update.

-Andrew

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Calling an Api Express from server code

It looks like you send "pUserID" and "pPasscode" as headers instead of sending them as parameters. Please look at the link above to get an example of usage these parameters, e.g.:

prevar XHRResponse = XHR2.send("PUT", "https://api.appery.io/rest/1/apiexpress/api/svcLogin/SetPasscode", {
"parameters": {
"pUserID": "1",
"pPasscode" : "abcdef"
},
"headers": {
"X-Appery-Api-Express-Api-Key" : "xxxxx"
}
});/pre

Andrew6784445
Posts: 1
Joined: Fri Apr 04, 2014 2:08 pm

Calling an Api Express from server code

Sergiy,

Thanks for the reply. I have updated my APIExpress Service to accept the query parameters as parameters instead of headers. When tested in API Express it works fine. When calling from Server Code using the following code I get no errors but also continue to not see results. Is there some sort of security that prevents an update query from being called by Server Code?

(Here is my revised code copied and pasted-except for API Key of course)
var XHRResponse = XHR2.send("PUT", "https://api.appery.io/rest/1/apiexpre...", {

"parameters": {
"pUserID": "1",
"pPasscode": "abcdef"
},

"headers": {
"X-Appery-Api-Express-Api-Key": "xxxxxx"

}
});

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Calling an Api Express from server code

You have to read a response of that API Express call.
Please look at this example how to do it: https://docs.appery.io/reference#sect...

Return to “Issues”