B4J Question Web Server Handler

Pedro Caldeira

Active Member
Licensed User
Hello All,

I am playing with the jServer lib.
How can I make a handler that receives a parameter and returns a code to the client ?

For a simple example that I want to do:

Service Handler named payment that accepts a money amount

127.0.0.1:8888/payment 00000500
The service catches the 00000500 and returns
You have entered 00000500
 

Pedro Caldeira

Active Member
Licensed User
Since I don't know exactly what the client sends Can I get whatever it is and log it, for example ?
if so, how ?
 

OliverA

Expert
Licensed User
Look at the request (ServletRequest) object properties and methods. In the "Guess My Number" example, the handler is defined as
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
The req object contains the request (the client data).
req.InputStream would give you the data that the client sent you (in raw format)
req.ParameterMap would give you all the parameters that the client sent you (either through the URI (via GET request) and/or body (via POST request))
req.FullRequestURI would show you the URL used to access your page
req.GetMultiPartData will help you with request that were POSTed multipart

You can log any of these (and anything else) to see what is coming in
 

Pedro Caldeira

Active Member
Licensed User
There is something that I dont understand in jServer with the Handlers
I want a Webserver where I can acess via json and to send some parameters.
i dont want web pages or input numbers via html.
I wan to mimmic a payment terminal in order to debug a pos application
The original webserver in the pament terminal accepts for instance:

https://127.0.0.1:8888/?operation=S...r&password=pass&type=4&posid=pos1&parameters={"amount":"100"}

can I do this with jServer ?
 

OliverA

Expert
Licensed User
B4X:
Dim paramMap as Map = req.ParameterMap
' paramMap.Get("name") should return the string "user"
' paramMap.Get("parameters") should return string "{"amount":"100"}"
Dim JSON as JSONParser
JSON.Initialize(paramMap.Get("parameters"))
Dim myJSONmap as map = JSON.NextObject ' {} make it an object, [] an array. Without either, it's just a value.
log(myJSONmap.Get("amount"))
Note about code: Not tested, but that should be the gist of it

Depending on how the JSON is encoded, you need to use either NextArray, NextObject or NextValue methods of the JSONParser class. Using the wrong method will most likely throw an error. As to encoding the username and password in the URL, that should be avoided. Most web servers keep a log of URL's accessed and then both the username and the password would be available in plain text. If you post the username and password, they will not show up in logs. Without SSL (or some other means of encryption), the username and password are still transmitted in plain text across the network.
 
Top