B4J Question Error in Jserver and POST method

Discussion in 'B4J Questions' started by Marcos Alves, Apr 18, 2019.

  1. Marcos Alves

    Marcos Alves Active Member Licensed 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:
    Code:
    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:

    Code:
    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?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Marcos Alves likes this.
  3. Marcos Alves

    Marcos Alves Active Member Licensed User

    ok @Erel ,

    I just tested with the code:
    Code:
    Dim postMap As Map   
                
    Dim FullUri As String = req.FullRequestURI       
                postMap = req.GetMultipartData(
    File.DirApp,10000000000000)
    And Post call:
    Code:
    <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?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    There is a confusion here with two protocols: application/x-www-form-urlencoded and multipart/form-data.

    Set the form encoding to multipart:
    Code:
    <form action="http://127.0.0.1:51042/pushserver" method="post" id="usrform" enctype="multipart/form-data">
     
    Marcos Alves likes this.
  5. Marcos Alves

    Marcos Alves Active Member Licensed User

    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!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice