Rlabkey - HTTP error code 500 using executeSql function

LabKey Support Forum
Rlabkey - HTTP error code 500 using executeSql function jpellet  2024-01-12 01:57
Status: Active
 
> sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Rlabkey_3.1.0

I got an error message (see below) using the executeSql function in Rlabkey.

test<-labkey.executeSql(
+     baseUrl="http://localhost:8080/labkey", 
+     folderPath = "/DEMO",
+     colNameOpt = "rname",
+     schemaName = "lists",
+     sql="SELECT * from Test")

Error in handleError(response, haltOnError) :
HTTP request was unsuccessful. Status code = 500, Error message = Error on line 1: Syntax error near 'U0VMRUNUJTIwKiUyMGZyb20lMjBUZXN...'

If I use the selectRows function I have the correct result:

> labkey.selectRows(
+     baseUrl="http://localhost:8080/labkey",
+     folderPath="/DEMO",
+     schemaName="lists",
+     queryName="Test")
  Patient ID GENDER Date
1       1022   Male      2016-08-08
2       1030   Male      2016-11-25
> 

I have checked all the logs and server configuration and I don't understand why the function executeSql does not work.

Thank you for your feedback.

 
 
mohara responded:  2024-01-12 17:44

Hi, and thanks for reaching out.

I took the liberty of reformatting your message and also truncating the syntax error string which looks odd to me. Is there any chance that's an API key you are using to authenticate? I wouldn't expect a key to ever appear in an error message like that and can't reproduce that happening, but it looks like something generated...

As some general troubleshooting steps, what happens if you drop the line setting colNameOpt from the executeSql? What if you add it to the selectRows? Do any of your field names contain a special character or are they excessively long perhaps? If you try only selecting one named column in your sql, does that succeed? Select * will also include 'internal' or system columns, which you may not want to be loading anyway.

test<-labkey.executeSql(
     baseUrl="http://localhost:8080/labkey", 
     folderPath = "/DEMO",
     schemaName = "lists",
     sql="SELECT Date from Test")

Another consideration is how you are authenticating. If you are using an API key or netrc file, have you tried directly authenticating using labkey.setDefaults in RStudio to see if there is a different result? As this is on your local machine, can you try adding another user account to see if the same behavior happens (and if the odd error code message is the same)?

To help any further, I'd need a bit more information from you. What version of LabKey are you running? Did you confirm that you are using (supported versions of tomcat, Java, and your database](https://www.labkey.org/Documentation/wiki-page.view?name=supported)?

--Molly

 
jpellet responded:  2024-01-16 03:21

Dear Molly,

thank you for your reply.

Yes I use an API key to authenticate but the Syntax error 'U0VMRUNUJTIwKiUyMGZyb20lMjBUZXN...' does not match my API key.
I tested the same code with an other user account (so an other API key) and the message is exactly the same, with the same Syntax error.

I also tried to authenticate using labkey.setDefaults in RStudio with email and password, and I got the same error message.

I dropped the line setting colNameOpt from the executeSql, same Error message. I put colNameOpt to the selectRows and I got the correct result:

    baseUrl="http://localhost:8080/labkey",
    folderPath="/DEMO",
    schemaName="lists",
    queryName="Test",
    colNameOpt = "rname") 
 patient_id gender Date
1       1022   Male      2016-08-08
2       1030   Male      2016-11-25 ```

If I change the sql query to only select Gender using executeSql I get the same error message:

```test<-labkey.executeSql(
    baseUrl="http://localhost:8080/labkey", 
    folderPath = "/DEMO",
    schemaName = "lists",
    sql="SELECT gender from Test")
Error in handleError(response, haltOnError) : 
 HTTP request was unsuccessful. Status code = 500, Error message = Error on line 1: Syntax error near 'U0VMRUNUJTIwZ2VuZGVyJTIwZ....'

If I make a deliberate mistake on the sql query I get the same error.

Labkey version 22.11.0
Tomcat 9.0.83
Postgresql 13.13
Java 17.0.9

 
mohara responded:  2024-01-16 18:15

Thanks for that additional information and yes, it turns out there's a compatibility issue with those versions of R and Rlabkey with old versions of LabKey (including 22.11) causing this exact error. I'm able to reproduce this locally against 23.7 and didn't go back any farther. I'm not certain exactly what the issue is here, and it's possible that downgrading to using R version 3.x.x might resolve it, but that could add other issues so I didn't try this option myself.

Better yet, I can confirm that it is fixed/compatible in LabKey Server version 23.11, and will work with your versions of R, RStudio, Rlabkey, etc as detailed above. Since we recommend staying current with the latest LabKey Server release anyway, that's the best way forward.

You can find the latest release of LabKey Server 23.11 on our website here:

Note that to use an API key to authenticate against LabKey Server 23.11, you also need to have a system default domain set. It can be left set to the default "labkey.com" or set to your own domain. All this does is allow "users" (including the APIkey) to authenticate without also including an email account domain. If you try your executeSql after the upgrade using a newly generated API key and receive an authentication error, this would be the first thing to check.

Hope that helps,

--Molly

 
mohara responded:  2024-01-17 08:56

It turns out this is a known issue: In Rlabkey version 3.0, we started encoding the parameters to executeSql to avoid WAF rejection, and this is not compatible with versions of LabKey Server prior to 23.9. You can restore the prior behavior by including this before the call to executeSql:

labkey.setWafEncoding(FALSE)

Upgrading to 23.11 will also address this issue, and is still the recommended overall plan to remain current with all fixes and upgrades.

My apologies for missing this the first time as it is included in the Rlabkey release notes, which are now linked from our documentation as well:

--Molly