B4J Question Server Not Receiving JSON Request

cklester

Well-Known Member
Licensed User
For some reason, my server is apparently not receiving the posted JSON string from my client.

The client app sends with this code:

B4X:
Sub bttn_Register_Click
    Dim bc As BCrypt

    Dim uname As String = txt_EmailLogin.Text.Trim
    Dim pwd As String = txt_LoginPassword.Text.Trim
    
' hash password
    Dim salt As String = bc.gensalt
    Dim hashed As String = bc.hashpw(pwd,salt)

    Dim strData As String
    Dim parser As JSONParser
    
' attempt register at site
    Dim http As HttpJob
    Dim param As Map = CreateMap("email":uname,"pwd":hashed,"test":"thisisatest")
    Dim json As String
    Dim jgen As JSONGenerator
    jgen.Initialize(param)
    json = jgen.ToString

    Log(salt)
    Log(hashed)
    Log(json)

    http.Initialize("",Me)
    http.PostString("http://127.0.0.1:51042/register",json)
    
    wait for (http) JobDone(http As HttpJob)
    If http.Success Then
        strData = http.GetString
        Log("strData=" & strData)
        parser.Initialize(strData)
        Dim Map1 As Map = parser.NextObject
        If Map1.Get("success") Then
            Log("success")
'            B4XPages.ClosePage(Me)
        Else
            Log("errorMessage=" & Map1.Get("errorMessage"))
            Return
        End If
    Else
        Log("Http job not success")
    End If
End Sub

It logs the following:

B4X:
$2a$10$YGSbRMfhDNB8rYMhrjcJoe
$2a$10$YGSbRMfhDNB8rYMhrjcJoeaI3klj6b171LPbs4TDj3f//K5wN6Zp2
{"test":"thisisatest","pwd":"$2a$10$YGSbRMfhDNB8rYMhrjcJoeaI3klj6b171LPbs4TDj3f\/\/K5wN6Zp2","email":"[email protected]"}
strData={"success":true,"errorMessage":""}
success

The server is receiving with this code:

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Private success As Boolean
    success = False
    
    resp.ContentType = "application/json"
    
    Dim j As HttpJob
    j.Initialize("j", Me)

    Dim jg As JSONGenerator
    Dim responeMap As Map
    
    Log("req.FullRequestURI")    
    Log(req.FullRequestURI)
    Log("req.ParameterMap")
    Log(req.ParameterMap)
    Log("email from ParameterMap=" & req.ParameterMap.Get("email"))
    
    Dim userName As String = req.GetParameter("email")
    Dim password As String = req.GetParameter("pwd")
    
    Log("test=" & req.GetParameter("test"))
    Log("email=" & userName)
    Log("pwd=" & password)
    
    If DB.CheckUserExist(userName) Then
        errorMessage = "Name already taken."
    Else
'        DB.AddUser(userName, password)
        success=True
    End If

    '(debugger will not stop in the following code)
    responeMap.Initialize
    responeMap.Put("success", success)
    responeMap.Put("errorMessage", errorMessage)
    jg.Initialize(responeMap)
    resp.Write(jg.ToString)
End Sub

The output from the server when receiving the request is as follows:

B4X:
req.FullRequestURI
http://127.0.0.1:51042/register
req.ParameterMap
(MyMap) {{"test":"thisisatest","pwd":"$2a$10$GyIdRb\/ByldfrYFWpyW8yeoyaxQhOanCLl6O9QG1LfcZuigerPN2S","email":"[email protected]"}=[Ljava.lang.String;@713464d9}
email from ParameterMap=null
test=
email=
pwd=

I see something unexpected (to me) for the req.ParameterMap, but maybe that's expected. Looks like it's still a string...(?)

Can anybody give me a hint as to why the server is apparently not receiving the posted JSON string or is just unable to parse it?
 

KMatle

Expert
Licensed User
Longtime User
(MyMap) {{"test":"thisisatest","pwd":"$2a$10$GyIdRb\/ByldfrYFWpyW8yeoyaxQhOanCLl6O9QG1LfcZuigerPN2S","email":"[email protected]"}=[Ljava.lang.String;@713464d9

As I see this should be the string you've received. Initialize a jsonparser object with this string and you get a map.
 
Upvote 0

cklester

Well-Known Member
Licensed User
As I see this should be the string you've received. Initialize a jsonparser object with this string and you get a map.

I'm confused because req.ParameterMap is not a string. It's a map. I just did some debugging and discovered it is a map and this is the first (and only) key in it:

B4X:
req.ParameterMap
key: {"test":"thisisatest","pwd":"$2a$10$OPbGg3fQqGZE2h1I091C2.YsJrSNgTKfX57UTnIVFBqDCp53jQtEK","email":"[email protected]"}
value:[Ljava.lang.String;@7dd93dc9

Is it supposed to be like that?
 
Upvote 0

cklester

Well-Known Member
Licensed User
So you have a map inside a map. A { means a map, a [ would be list. So just parse it twice (with the key this time). Better: Change the map you send to a proper format.

Yeah, I must be doing something wrong. The sample code I've derived mine from does not require any manual manipulation like that. You just stringify your JSON, send it through the POST request, and it pops out on the other side ready to give you the parameters.
 
Upvote 0

B4XDev

Member
Licensed User
The following seems to work:

Must Use Json Content-Type to Submit Json String to B4J Server:
http.Initialize("",Me)
http.PostString("http://127.0.0.1:51042/register",json)
'THE LINE BELOW IS REQUIRED FOR B4J SERVER TO KNOW IT IS JSON NOT STRING PARAMETERS
http.GetRequest.SetContentType("application/json")
 
Upvote 0
Top