B4J Question Error in Jserver and POST method

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello all,

I'm trying to receive data in a B4J server using a POST method. To do this I created a local form like this:
B4X:
html>
<html>
<body>
<form action="http://127.0.0.1:51042/pushserver" method="post" id="usrform">
idUser: <input type="text" name="username"><br>
Token: <input type="text" name="token"><br>
sessionId: <input type="text" name="sessionid"><br>
<input type="submit">
</form>
<textarea name="data" form="usrform"></textarea>
</body>
</html>
</html>

Open this form in Chrome and, running the java server, it receives the POST call in this code:

B4X:
req As ServletRequest
Dim postMap As Map
postMap.Initialize
postMap = req.GetMultipartData(File.DirApp,1000000000)
Log( "sessionid -> " & req.GetParameter("sessionid") )

The POST event is arriving, the req.ContentType is correct (application/x-www-form-urlencoded), the req.method is POST but I can't recover the POST fields or values, doesn't matter what method I use (postMap is empty and GetParameter - that I know that should be used for GET method, but tested...) .
What am I doing wrong?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Never initialize a variable before you assign a different object to the same variable.
B4X:
Dim postMap As Map = req.GetMultipartData(...)

The form fields are added to the map: https://www.b4x.com/android/forum/threads/postmultipart-handle-on-b4j-server.103487/#post-648633
ok @Erel ,

I just tested with the code:
B4X:
            Dim postMap As Map   
            Dim FullUri As String = req.FullRequestURI       
            postMap = req.GetMultipartData(File.DirApp,10000000000000)

And Post call:
B4X:
<html>
<html>
<body>
<form action="http://127.0.0.1:51042/pushserver" method="post" id="usrform">
idUser: <input type="text" name="username"><br>
Token: <input type="text" name="token"><br>
sessionId: <input type="text" name="sessionid"><br>
<input type="submit">
</form>
<textarea name="data" form="usrform"></textarea>
</body>
</html>
</html>

But still getting a zero size Map...
upload_2019-4-19_10-34-33.png

An interesting thing is changing to GET and recovering the parameters with req.GetParameter is working...

How can I debug this to find out the problem? Really don't know. I already opened the req object and its big! I couldn't find the parameters MAP inside this even to at least check if they are in the object after receiving the POST... what can I do to debug?
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
There is a confusion here with two protocols: application/x-www-form-urlencoded and multipart/form-data.

Set the form encoding to multipart:
B4X:
<form action="http://127.0.0.1:51042/pushserver" method="post" id="usrform" enctype="multipart/form-data">
Thanks @Erel . My mistake, it was a simple solution! I didn't notice that because when using the multipartpost methods called FROM B4X app, the server written in php recognizes application/x-www-form-urlencode AND multipart/form-data with the same code (I actually use forms to test the codes those will be called by htpmultipart requests by apps)...

I'll fix the form code! Thanks master!
 
Upvote 0
Top