XojoCloud app webservice

I am using a Web 2.0 app in xojocloud as web service. It is supposed to get a POST from an URLconnection on a desktop Xojo app in order to record some items in a PostgreSQL database (in xojocloud). Every works fine when testing in local host but not when deployed in xojocloud. Do I need to open a port on the xojocloud firewall? If so, which port?

If you are using HandleUrl then there’s no port to open.

That said, your request must use the URL and not the IP address.

If you are using SSL, you will need your desktop app to use https as well.

Thanks. Yes I am using HandleURL and http (like http://myapp.xojocloud.net/InsertRecord)

Any clue why it works on local host but not when running in Xojo Cloud?

Hi Juan,

Here’s a sample of some db connection code as an example… if you paste what you’re using perhaps we can see where things have gone wrong :slight_smile:

If TargetXojoCloud Then
  
  // Running on Xojo Cloud
  
  Var db As New MySQLCommunityServer
  db.UserName = "api_app"
  db.Password = app.api_app_password
  db.Host = "127.0.0.1"
  db.Port = 3306
  db.DatabaseName = "fluidlink_io"
  Try
    db.Connect
    // You're connected!
    MessageBox("FLUIDLINK_IO production db OK")
  Catch error As DatabaseException
    MessageBox("FLUIDLINK_IO production db BAD")
  End Try
  
Else
  
  // Not on Xojo Cloud
  
  Var db As New MySQLCommunityServer
  db.UserName = "api_app"
  db.Password = app.api_app_password
  db.Host = "127.0.0.1"
  db.Port = 3306
  db.DatabaseName = "fluidlink_io"
  Try
    db.Connect
    // You're connected!
    MessageBox("FLUIDLINK_IO dev db OK")
  Catch error As DatabaseException
    MessageBox("FLUIDLINK_IO dev db BAD")
  End Try
  
End If

Here is code on web app running as a web service
App HandleURL:
If Request.Path <> “” Then
Var data As String = Request.Body.DefineEncoding(Encodings.UTF8)
Var entrada As JSONItem = new JSONItem(data)
Select case Request.Path
Case “InsertarPrueba”
InsertarPrueba(entrada.Value(“prueba”))
Return True
End Select
End if

InsertarPrueba Method (item As JSONItem):
Var codigo As String = item.Value(“codigo_examen”)
Var nombre As String = item.Value(“nombre_examen”)
Var fechaorden As DateTime = item.Value(“fecha_orden”).DateTimeValue
Var fecharesultado As DateTime = item.Value(“fecha_resultado”).DateTimeValue
Var resultado As String = item.Value(“resultado”)

Var row As new DatabaseRow
row.Column(“codigo_examen”) = codigo
row.Column(“nombre_examen”) = nombre
row.Column(“fecha_orden”) = fechaorden
row.Column(“fecha_resultado”) = fecharesultado
row.Column(“resultado”) = resultado

Try
App.DB.AddRow(“pruebas”, row)
Catch error As DatabaseException
MessageBox("DB Error: " + error.Message)
End Try

App Opening:
DB = New PostgreSQLDatabase
DB.Port = 5432
#if TargetXojoCloud then
DB.DatabaseName = “DatabaseName”
DB.Host = “localhost”
DB.UserName = “Myusername”
DB.Password = “Mypassword”
#Else
DB.DatabaseName = “DatabaseName”
DB.Host = “localhost”
DB.UserName = “Myusername”
DB.Password = “Mypassword”
#Endif
Try
DB.Connect
Catch error As DatabaseException
End If

The Desktop App on a button action:
Var d as New Dictionary
d.Value(“codigo_examen”) = TextField1.Text
d.value(“nombre_examen”) = TextField2.Text
d.Value(“fecha_orden”) = DateTimePicker1.SelectedDate.SQLDateTime
d.Value(“fecha_resultado”) = DateTimePicker2.SelectedDate.SQLDateTime
d.Value(“resultado”) = PopupMenu1.SelectedRow
Var item as new JSONItem
item.Value(“prueba”) = d

URLConnection1.SetRequestContent(item.ToString,“application/json”)
URLConnection1.Send(“POST”,“http://mywebservice.xojocloud.net/InsertarPrueba”)

Yes, but you’re explicitly ignoring the clues by not handling them with the try catch. When you catch an exception, you should do something with it like log or notify.

Thanks. Not really familiar with notify or making a log for a PostgreSQL database. I removed some message boxes from the code (my usual way to see catched errors) because I believe (not sure of this) that being a web service, the web app is going to be always running on my Xojo Cloud server so no user is going to execute it in a browser so he can see the messages displayed.

Maybe go back to the beginning, make a quick ‘test’ app for the web. Put a simple button on a page and put your PostgreSQL connection code there, with a simple dialog for success/fail. Then work on the connection code until you resolve it. Maybe the db.password db.username on the host are different than your local dev db? It appears you have the same port# used, and that’s the port# that Xojo Cloud lists in their docs, so that may not be the issue. I’d focus on finding out if the user account you use on your local db has a matching account in the cloud-hosted db. (sorry for the rambling, I’m just about to run out the door and wanted to give you a brain dump)

Thanks. I have another web App with the same routine to access that same database with the same connection code and everything works fine. The only difference is that in the web app the DB property is part of Session as well as the routine for db connection is in the Session opening event. In the web service app, the DB property is part of App and the routine for db connection is in the App opening event

For a web service you’ll be better off making your own client session mechanism with classes for each client so the database usage from two different clients don’t clash with one another. You would then have a unique database connection for each client that you hook up when a request comes in and then tear down after a response is made.

1 Like

Thanks. At the end, the problem was with privileges in the database. Everything now works.