Android Question Connecting to influxDB from B4x

emilio55

New Member
Licensed User
Longtime User
Hello everyone,

i need to query a database influxDB and i'm using Postman successfully, making an http request with the following parameters:

endpointUrl = "https://mydomain.com:8086/api/v2/query?org=myorg"

body:

from(bucket: "example-buket")
|> range(start: -1h)
|> filter(fn: (r) => r["_measurement"] == "meteo_garden")
|> filter(fn: (r) => r["_field"] == "METEO_GardenWS_AH.PV")

Header:

Accept: application/csv
Content-type: application/vnd.flux
Authorization: Token l3uZ4CVH_...........................................................41t5K6PHuYalg==

Now i'm trying to use B4X with okHTTP, but my request is not compiled and i get an error message. This is my code:

influxDB connection:
Sub Globals
    Dim endpointUrl As String = "https://mydomain.com:8086/api/v2/query?org=myorg"
    
'    Dim Body As String =     $"from(bucket: 'example-buket')
'      |> range(start: -1h)
'      |> filter(fn: (r) => r['_measurement'] == 'meteo_garden')
'      |> filter(fn: (r) => r['_field'] == 'METEO_GardenWS_AH.PV')"$
    
    Dim Body As String = "from(bucket: 'example-buket')\r\n  |> range(start: -2d)\r\n  |> filter(fn: (r) => r['_measurement'] == 'meteo_garden')\r\n  |> filter(fn: (r) => r['_field'] == 'METEO_GardenWS_AH.PV')\r\n"
    Dim authCode As String = "Token l3uZ4CVH_...................41t5K6PHuYalg==" '<- Put your auth code here

End Sub
'
Sub Activity_Create(FirstTime As Boolean)
    
    Dim ok As HttpJob
    ok.Initialize("",Me)
    ok.PostString(endpointUrl, body)
    ok.GetRequest.SetContentType("application/vnd.flux")
    ok.GetRequest.SetHeader("Authorization", authCode)
    ok.GetRequest.SetHeader("Content-Type", "application/vnd.flux")
    ok.GetRequest.SetHeader("Accept", "application/csv")
    '
    Wait For (ok) Jobdone(ok As HttpJob)
    If ok.Success Then
        Log("Success")
        Log(ok.GetString)
    Else
        Log("FAiled")
        Log(ok.ErrorMessage)
    End If
    ok.Release
End Sub


I'm almost sure i'm parsing the body in a wrong way .... the errore message i get is the following

{"code":"invalid","message":"compilation failed: error @1:6-1:20: invalid expression @1:20-1:21: '\n\nerror @1:15-1:20: invalid expression @1:14-1:15: '\n\nerror @3:14-3:59: invalid expression @3:59-3:60: '\n\nerror @3:25-3:42: invalid expression @3:40-3:41: '\n\nerror @3:28-3:40: invalid expression @3:27-3:28: '\n\nerror @3:46-3:47: invalid expression: invalid token for primary expression: ILLEGAL\n\nerror @3:47-3:59: invalid expression @3:46-3:47: '\n\nerror @4:14-4:61: invalid expression @4:61-4:62: '\n\nerror @4:25-4:36: invalid expression @4:34-4:35: '\n\nerror @4:28-4:34: invalid expression @4:27-4:28: '\n\nerror @4:40-4:41: invalid expression: invalid token for primary expression: ILLEGAL\n\nerror @4:41-4:58: invalid expression @4:40-4:41: '"}

Can anyone help me? Thank you very much in advance!
 
Solution
from(bucket: "example-buket")
|> range(start: -1h)
|> filter(fn: (r) => r["_measurement"] == "meteo_garden")
|> filter(fn: (r) => r["_field"] == "METEO_GardenWS_AH.PV")
Try:

B4X:
    Dim Body As String = $"from(bucket: "example-buket")
|> range(start: -1h)
|> filter(fn: (r) => r["_measurement"] == "meteo_garden")
|> filter(fn: (r) => r["_field"] == "METEO_GardenWS_AH.PV")"$

emilio55

New Member
Licensed User
Longtime User
P.S.: i have noticed that the code editor here probably contains a bug: when it find a ' (apostrophe) inside a string marqued by " " all the following text is a green comment.
The ide in B4X works fine and dont make all as a comment.
 
Upvote 0

emilio55

New Member
Licensed User
Longtime User
Use String Literals.
eg.
B4X:
MyString = $"Some text with character or 'symbols' that need to be escaped"$
Thank you, Aeric, but i have the same error.
The query works fine in Postman and generate the following code:

Postman query:
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("application/vnd.flux");
RequestBody body = RequestBody.create(mediaType, "from(bucket: \"mybuket\")\r\n  |> range(start: -1h)\r\n  |> filter(fn: (r) => r[\"_measurement\"] == \"example_measurement\")\r\n  |> filter(fn: (r) => r[\"_field\"] == \"example_field\")\r\n");
Request request = new Request.Builder()
  .url("https://mydomain.com:8086/api/v2/query?org=myorg")
  .method("POST", body)
  .addHeader("Authorization", "Token l3u..............6PHuYalg==")
  .addHeader("Accept", "application/csv")
  .addHeader("Content-Type", "application/vnd.flux")
  .build();
Response response = client.newCall(request).execute();

I'm almost sure i'm parsing the body in a wrong way using B4X, because B4X dont show error and send the request
but this one is not compiled and give me the previous error.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
from(bucket: "example-buket")
|> range(start: -1h)
|> filter(fn: (r) => r["_measurement"] == "meteo_garden")
|> filter(fn: (r) => r["_field"] == "METEO_GardenWS_AH.PV")
Try:

B4X:
    Dim Body As String = $"from(bucket: "example-buket")
|> range(start: -1h)
|> filter(fn: (r) => r["_measurement"] == "meteo_garden")
|> filter(fn: (r) => r["_field"] == "METEO_GardenWS_AH.PV")"$
 
Upvote 0
Solution
Top