For some reason, my server is apparently not receiving the posted JSON string from my client.
The client app sends with this code:
It logs the following:
The server is receiving with this code:
The output from the server when receiving the request is as follows:
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?
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":"me@mail.com"}
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":"me@mail.com"}=[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?