B4J Question jShell parameter is ignored

avalle

Active Member
Licensed User
Longtime User
Hello!
I'm trying to use cURL via jShell to overcome the lack of support of mutual TLS authentication in jServer. See https://www.b4x.com/android/forum/t...s-client-authentication-in-b4j-server.116452/
It looks like some parameters of the curl.exe call are ignored.
I also tried to add the "-v" parameter to obtain a verbose log of the TLS handshaking, but it's also ignored.
jShell with cURL:
Sub AppStart (Args() As String)
    Dim shl As Shell
    
    shl.Initialize("shl", "C:\Program Files\cURL\bin\curl.exe", _
       Array As String("-i", "-v", "-L", "https://b4x.com"))
    shl.WorkingDirectory = File.DirApp
    
    shl.Run(10000) 'set a timeout of 10 seconds
    StartMessageLoop 'need to call this as this is a console app.
End Sub
Does anyone have an idea why this is happening?
My suspect is that also other parameters may be ignored, impacting the success of mTLS.

Thanks
Andrea
 

drgottjr

Expert
Licensed User
Longtime User
did you consume the shl's event?

output i got:


i don't use curl, but i think the -i and -v arguments give the response headers and verbose output. i seem to have gotten them. and, of course, the -L was not ignored. you didn't say what you got when you ran shl, so i can't comment further.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The handshake information is piped to STDERR, not STDOUT.
B4X:
'Non-UI application (console / server application)
#Region Project Attributes 
    #CommandLineArgs:
    #MergeLibraries: True 
#End Region

Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    doCurl
    StartMessageLoop
End Sub

Sub doCurl
    Dim shl As Shell
    shl.Initialize("shl", "C:\B4X\curl-7.75.0-win64-mingw\bin\curl.exe", Array As String("-i", "-v", "-L", "https://b4x.com"))    'Perform an ImageMagic Conversion.
    shl.WorkingDirectory = File.DirApp
    shl.Run(10000) 'set a timeout of 10 seconds
    Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success And ExitCode = 0 Then
        Log("Success")
        'Log(StdOut)
        Log(StdErr) 'Log TLS handshaking information
    Else
        Log("Error: " & StdErr)
    End If
    StopMessageLoop
End Sub
 
Upvote 0

avalle

Active Member
Licensed User
Longtime User
That's it Oliver! Thanks!
I didn't know that the handshaking info goes to STDERR.
Clearly when using the console everything goes there...

With that now I'll figure out why mTLS does not work as expected within the jShell call.
Will update you shortly...
 
Upvote 0

avalle

Active Member
Licensed User
Longtime User
Ok so the problem I'm facing is with the encoding of the data parameter for Curl on Windows:
cURL sample:
"C:\Program Files\cURL\bin\curl.exe" https://postman-echo.com/post -H "Accept: application/json" -H "Content-Type: application/json; charset=UTF-8" -d "{\"token\":\"test123ABC\"}"
The "-d" parameter is not properly encoded when using the following code in B4J:
doCurl:
shl.Initialize("shl", "C:\Program Files\cURL\bin\curl.exe", _
    Array As String("-H", $""Accept: application/json""$, "-H", $""Content-Type: application/json; charset=UTF-8""$, _
    "-d", $""{\"token\":\"test123ABC\"}""$, "https://postman-echo.com/post"))
Looking at the response from Postman:
Postman Echo:
{"args":{},"data":"'{\"client_notification_token\":\"test123ABC\"}'","files":{},"form":{},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-60282d6b-2e1bea5024b36e486199858f","content-length":"44","user-agent":"curl/7.75.0","accept":"application/json","content-type":"application/json; charset=UTF-8"},"json":null,"url":"https://postman-echo.com/post"}
"data" contains the value that is transmitted, which is an invalid JSON, and in fact the "json" field in the response is null.

I've tried other combinations of single quotes and double quotes, but still I can't understand how to fix it...
Your help is welcome!

Andrea
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Use InitializeDoNotHandleQuotes instead of Initialize
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…