Have to train "him" to write correct server code.
AE: Here is my code for Web API server...
CG: It looks like your code is using the B4X Server class to create a web server ...
srvr.AddHandler("/hello", "GET", AddressOf Hello)
AE: the third argument in AddHandler is a Boolean variable name SingleThreadHandler
CG: <some BS>
AE: most of the case i will use False for this variable
CG: <more BS>
AE: now build a hello world web api using b4x server
CG: Here is an example of how you can use the B4X Server class to create a simple "Hello, World!" web API:
Sub Process_Globals
'Declare global variables here.
Dim srvr As Server
End Sub
Sub AppStart (Args() As String)
'Start the server and define the API endpoints.
srvr.Initialize("")
srvr.Port = 8080
srvr.AddHandler("/hello", "GET", False, AddressOf Hello)
srvr.Start
End Sub
Sub Hello(req As HttpRequest, resp As HttpResponse)
'Handle requests to the /hello endpoint.
resp.ContentType = "text/plain"
resp.Write("Hello, World!")
End Sub
AE: the second argument should be a handler class name in string ...
CG: <BS again>
srvr.AddHandler("/hello", "GET", False, AddressOf Hello)
AE: no, the second argument is to specify the name of the Server Handler class instead of the HTTP method like "GET"
CG: <continue BS and same wrong code>
AE: no, you are wrong. you don't need to specify the "GET" method in `AddHandler` method. You can access the HTTP method using Request.Method
CG: You are correct that the HTTP method of a request can be accessed using the Method property of the HttpRequest object in B4X. This property returns a string value that specifies the HTTP method of the request, such as "GET", "POST", "PUT", "DELETE", etc.
Therefore, you do not need to specify the HTTP method as the second argument to the AddHandler method of the Server class. Instead, you can use the Method property of the HttpRequest object to determine the HTTP method of the request in your handler subroutine or function.
For example, here is how you could modify the Hello subroutine to handle different HTTP methods using the Method property of the HttpRequest object:
Sub Hello(req As HttpRequest, resp As HttpResponse)
'Handle requests to the /hello endpoint.
resp.ContentType = "text/plain"
Select req.Method
Case "GET"
resp.Write("Hello, World! (GET method)")
Case "POST"
resp.Write("Hello, World! (POST method)")
Case "PUT"
resp.Write("Hello, World! (PUT method)")
Case "DELETE"
resp.Write("Hello, World! (DELETE method)")
Case Else
resp.Write("Hello, World! (Unknown method)")
End Select
End Sub
AE: yes, now you get it!
CG: I'm glad that I was able to provide a correct and helpful answer to your question. If you have any other questions, please don't hesitate to ask. I will do my best to help.
AE: so you should write the following correct code at AppStart sub:
Server.AddHandler(ROOT_PATH & "hello/*", "HelloHandler", False)
CG: <continue BS and repeat wrong code>
Sub AppStart (Args() As String)
'Start the server and define the API endpoints.
srvr.Initialize("")
srvr.Port = 8080
srvr.AddHandler(ROOT_PATH & "hello/*", "GET", False, AddressOf HelloHandler)
srvr.Start
End Sub
Sub HelloHandler(req As HttpRequest, resp As HttpResponse)
'Handle requests to the /hello/* endpoint.
resp.ContentType = "text/plain"
resp.Write("Hello, World!")
End Sub
At Last...
AE: NO, you are still not correct. Server.AddHandler accepts three arguments.
The correct code is:
Server.AddHandler("hello/*", "HelloHandler", False)
The first argument accepts the Path As String. The path can consist of wildcard with asterisk "*"
The second argument accepts the Class As String which is the Server Handler Class. We can add the Server Handler Class from the menu in B4J IDE. Click menu Project, Add New Module, Class Module, Server Handler. Enter the module name and click Ok. The Handler Class look like the following:
'Handler class
Sub Class_Globals
End Sub
Public Sub Initialize
End Sub
Sub Handle(req As ServletRequest, resp As ServletResponse)
End Sub
The third argument in AddHandler is a Boolean variable name SingleThreadHandler. Usually we just need to use False.
CG: