B4J Question Best Practice for creating PHP REST APIs for beginners

Mashiane

Expert
Licensed User
Longtime User
Hi there

I am interested in first learning how to create REST APIs, however before I delve into how its done for jetty VPS case, let me understand for the shared hosting possibilities via PHP first.

So the first part is PHP, perhaps NodeJS in the near future.

Yesterday I got to study @aerics code for the PHP Login API, complicated for beginners but I find it beautifully structured. It's a masterpiece.

I on the other hand would like to start from a point of understanding first, why one thing is done, how to do one thing, how to apply CRUD, using JWT etc etc.

Thanks in advance.

PS: If you can recommend a resource to learn from, a book perhaps, a very good "explain it to your grandma" video perhaps, anything, etc, that would help. šŸ™

BTW, I don't know PHP that much so its important that for this process the starting point is CORE, no framework, nada.
 
Last edited:

alwaysbusy

Expert
Licensed User
Longtime User
This is an excellent resource: https://www.restapitutorial.com/

And implementing a REST API in a jServer is dead easy.

B4X:
'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

Alwaysbusy
 
Upvote 1
Top