'Handler class
Sub Class_Globals
End Sub
Public Sub Initialize
End Sub
' GET /test/gettest
' POST /test/posttest
' body: {"data": "my data"}
Sub Handle(req As ServletRequest, resp As ServletResponse)
resp.ContentType = "application/json"
Dim Response As String
Dim PathType As String = req.RequestURI.SubString("/test/".Length)
Dim bodyCode As String
Dim body As TextReader
Select Case req.Method
Case "GET"
Select Case PathType
Case "gettest"
Dim mOUT As Map = CreateMap("value": "Get Test HTTPS Successful!")
Dim json As JSONGenerator
json.Initialize(mOUT)
Response = json.ToString
Case Else
resp.Status = 400
resp.Write($"{"errorCode": "1700", "errorMessage": "Bad Request"}"$)
Return
End Select
Case "POST"
Select Case PathType
Case "posttest"
' read json body
body.Initialize(req.InputStream)
bodyCode = body.ReadAll
If bodyCode.StartsWith("{") Then
Dim jsonP As JSONParser
jsonP.Initialize(bodyCode)
Dim m As Map = jsonP.NextObject
Dim Data As String = m.Get("data")
If Data = "" Then
resp.Status = 409
resp.Write($"{"errorCode": "1702", "errorMessage": "Data not found in body"}"$)
Return
End If
' return some json
Dim mOUT As Map = CreateMap("value": "POST Test HTTPS Successful!", "data": Data)
Dim json As JSONGenerator
json.Initialize(mOUT)
Response = json.ToString
Else
resp.Status = 409
resp.Write($"{"errorCode": "1703", "errorMessage": "No Json body found"}"$)
Return
End If
Case Else
resp.Status = 400
resp.Write($"{"errorCode": "1700", "errorMessage": "Bad Request"}"$)
Return
End Select
Case "PUT"
...
Case "DELETE"
...
Case "PATCH"
...
Case Else
resp.Status = 400
resp.Write($"{"errorCode": "1701", "errorMessage": "Bad Request, Unsupported method ${req.Method}"}"$)
Return
End Select
If Response <> "" Then
resp.write(Response)
End If
End Sub