Android Question CURL trouble

Philip Prins

Active Member
Licensed User
Hello,

This is the info i get from the manufacturar:
Set Config
curl -v -d "single=<url>&double=<url>&long=<url>&touch=<url>" http://[IP]/api/v1/device/[MAC]

The order of parameters is not relevant. Request must have at least one parameter. URL to be sent must be encoded using x-www-form-urlencoded notation.
In order to make it possible and simple at the same time to specify in the URL that specific request should be sent as GET or POST we introduced a special (very unusual) conventions. URL is considered as get when it starts with get:// and as post when it starts with post:// (in both cases get and post replaces the usual http prefix). GET is sent as is and POST is sent such, that parameters from URL are sent as POST body.
I tried this:
B4X:
Dim S1 As String = ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S2 As String = ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S3 As String =  ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S4 As String =  ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress

Dim NV As Map
    NV.Initialize
    NV.Put("single", S1)
    NV.Put("double", S2)
    NV.Put("long", S3)
    NV.Put("touch", S4)
    
    Dim req As OkHttpRequest
    req = MultipartPost.CreatePostRequest("http://192.168.254.1/api/v1/device/"&ButtonMac, NV, Null)
    req.SetHeader("Content-Type", "application/json")
    req.SetContentType("application/json")
    req.SetContentEncoding("text/plain")
    
    hc.Execute(req, 1)
I also tried to make a JSON string and send it with POST string.
B4X:
    Dim S1 As String = "single="&ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S2 As String =  "double="&ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S3 As String =  "long="&ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S4 As String =  "touch="&ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress


Dim X As Map
    X.Initialize
    X.Put("single",S1)
    X.Put("double",S2)
    X.Put("long",S3)
    X.Put("touch",S4)
    
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(X)
    Dim R As String = JSONGenerator.ToPrettyString(2)
    
    Dim J As HttpJob
    J.Initialize("AST1",Me)
    j.PostString("http://192.168.254.1/api/v1/device/"&ButtonMac,R)
    j.GetRequest.SetContentType("application/x-www-form-urlencoded")

I always get as result SUCCES.

When i read the device i see this:
Strange characters instead of //

B4X:
response: anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpResponse@78dc88f
BSSID 62:01:94:27:ec:bd
{
    "single": "http:\/\/85.214.204.131:61042\/MyStrom?id",
    "double": "",
    "long": "",
    "touch": "",
    "generic": ""
}
What am i doing wrong??
 

DonManfred

Expert
Licensed User
OkHttpRequest
sounds like an mistake to use it at all.

I suggest to switch to okhttputils2 and not using the client directly!

Strange characters instead of //
/ must be escaped. It´s not strange

B4X:
Dim parser As JSONParser
parser.Initialize($"{
    "single": "http:\/\/85.214.204.131:61042\/MyStrom?id",
    "double": "",
    "long": "",
    "touch": "",
    "generic": ""
}"$)
Dim root As Map = parser.NextObject
Dim single As String = root.Get("single")
Dim double As String = root.Get("double")
Dim touch As String = root.Get("touch")
Dim long As String = root.Get("long")
Dim generic As String = root.Get("generic")
You should follow the route using httpjob.
 
Last edited:

Philip Prins

Active Member
Licensed User
sounds like an mistake to use it at all.

I suggest to switch to okhttputils2 and not using the client directly!


/ must be escaped. It´s not strange

B4X:
Dim parser As JSONParser
parser.Initialize($"{
    "single": "http:\/\/85.214.204.131:61042\/MyStrom?id",
    "double": "",
    "long": "",
    "touch": "",
    "generic": ""
}"$)
Dim root As Map = parser.NextObject
Dim single As String = root.Get("single")
Dim double As String = root.Get("double")
Dim touch As String = root.Get("touch")
Dim long As String = root.Get("long")
Dim generic As String = root.Get("generic")
Hello Don Manfred,

Can you point me in the right direction, been trying different things for the last 5 hours bu can not get the parameters in the device completely.
Tried String Utils to encode URL, tried so many things that i am completely lost now...
 

mangojack

Well-Known Member
Licensed User
Smart String Literals .. https://www.b4x.com/android/forum/threads/b4x-smart-string-literal.50135/

notice @DonManfred Parser.Initialize string in Post #2

for example ..
B4X:
Dim S1 As String = $"single=${ServerIP}:61042/MyStrom?id=${ButtonID}&fi=${FleetID}&tp=1&msg=${MessageTxt}&ads=${MessageAdress}"$
B4X:
j.PostString($"http://192.168.254.1/api/v1/device/${ButtonMac}"$,R)
Log the output to ensure it is as expected.
 
Last edited:

Philip Prins

Active Member
Licensed User
Top