Share My Creation [Project Template] Web API Server v3

Web API Server is now Pakai

Version: 3.50
Description: Web API Server with JSON response
Database: SQLite, MySQL

web-api-server-3.20.png


Template:
  • Web API Server (3.50).b4xtemplate
B4X Client App (B4A, B4i, B4J):
Depends on:
Please check this tutorial for guides.

Reminder: Please do not post any question on this thread.

Always remember to start a new thread for any question and tag "Web API Server v3".

You may also ask in the [Q&A] thread.
 

Attachments

  • WebApiUtils.b4xlib
    9 KB · Views: 65
  • MiniORMUtils.b4xlib
    13.3 KB · Views: 41
  • Web API Server (3.50).b4xtemplate
    427.9 KB · Views: 46
Last edited:

aeric

Expert
Licensed User
Longtime User
Updates:
29/10/2024
  • Template:
    • Web API Server (3.00beta4).b4xtemplate *updated
  • Depends on:
    • WebApiUtils.b4xlib (v3.01) *updated
    • MiniORMUtils.b4xlib (v1.14)
    • sqlite-jdbc-3.39.3.0.jar (or your preferred version)
    • mysql-connector-java-8.0.30.jar (or your preferred version)
Introduction of :
1. ElementMatch (Pattern As String) As Boolean
2. CropElements (FullElements() As String, StartingElementIndex As Int) As String() -> (WebApiUtils.b4xlib)

With these new methods, I am trying to simplify the way writing APIs which I think make more sense to newbie of this Web API framework.

Example:
B4X:
Sub Handle (req As ServletRequest, resp As ServletResponse)
    Request = req
    Response = resp
    Method = Request.Method.ToUpperCase
    Dim FullElements() As String = WebApiUtils.GetUriElements(Request.RequestURI)
    Elements = WebApiUtils.CropElements(FullElements, 3) ' 3 For Api handler
    Select Method
        Case "GET"
            If ElementMatch("") Then ' /api/categories
                GetCategories
                Return
            End If
            If ElementMatch("id") Then ' /api/categories/1
                GetCategoryById(ElementId)
                Return
            End If
        Case "POST"
            If ElementMatch("") Then ' /api/categories
                PostCategory
                Return
            End If  
        Case Else
            Log("Unsupported method: " & Method)
            ReturnMethodNotAllow
            Return
    End Select
    ReturnBadRequest
End Sub

Instructions: Download the updated template and library files in 1st post.
 

aeric

Expert
Licensed User
Longtime User
Updates:
30/10/2024

Template:
  • Web API Server (3.00rc1).b4xtemplate *updated
Depends on:
  • WebApiUtils.b4xlib(v3.02) *updated
  • MiniORMUtils.b4xlib (v1.14)
  • sqlite-jdbc-3.39.3.0.jar (or your preferred version)
  • mysql-connector-java-8.0.30.jar (or your preferred version)
What's New:
- Fix issue with SSL settings in Release
- Fix issue in FindApiHandler to check missing keywords key in request body
- Updated new Code Snippets to WebApiUtils for creating new Web and Api handlers

Instructions: Download the updated template and library files in 1st post.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Releases:
Template:
  • Web API Server (3.00).b4xtemplate *updated
Depends on:
  • WebApiUtils.b4xlib (v3.02)
  • MiniORMUtils.b4xlib (v1.14)
  • sqlite-jdbc-3.39.3.0.jar (or your preferred version)
  • mysql-connector-java-8.0.30.jar (or your preferred version)
What's New:
- Fix issue with SSL settings in Release
- Fix issue in FindApiHandler to check missing keywords key in request body
- Updated new Code Snippets to WebApiUtils for creating new Web and Api handlers
- Renamed JavaScripts files name and move from js to scripts folder inside www/assets

Instructions: Download the updated template and library files in 1st post.
Please remember to clear browser cache due to changes in JavaScript files.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Hi guys,
I have uploaded the wrong template file.
I was using RAR format in WinRAR. It will not work.
It is now fixed using ZIP format.

Please download the template again.

Sorry for any inconvenience.
 

aeric

Expert
Licensed User
Longtime User
Updates:
08/11/2024
  • Template:
    • Web API Server (3.10 beta 1).b4xtemplate
What's New:
  • Fix error in Main module (line #129) when selecting MySQL
  • New ServerUrl in ServerConfigurations type
  • ConfigurePort sub replaces ConfigureSSL
  • Remove ApplySettings sub
  • Updated HelpHandler
    • New VerbSection type to simplify some methods signature
    • GenerateVerbSection now accepting a single Verbsection type
    • GenerateDocItem now accept a single Map
    • GenerateLink sub updated using ServerUrl
    • GenerateNoApiLink sub added for adding non api handler to documentation
    • ReadHandlers sub updated
    • JavaScript updated
      • fade-out and fade-in animation on bootstrap alert when submitting the endpoint again
      • html elements using id instead of button id
      • Some code for basic and token authentication support
  • Next update will try to update html for release
Note: This is the first beta for v3.10 and it is expecting to contains some bugs.
 

aeric

Expert
Licensed User
Longtime User
Releases:
10/11/2024
  • Template:
    • Web API Server (3.10).b4xtemplate
What's New:
  • Fix error in Main module (line #129) when selecting MySQL
  • New ServerUrl in ServerConfigurations type
  • ConfigurePort sub replaces ConfigureSSL
  • Remove ApplySettings sub
  • Updated HelpHandler
    • New VerbSection type to simplify some methods signature
    • GenerateVerbSection now accepting a single Verbsection type
    • GenerateDocItem now accept a single Map
    • GenerateLink sub updated using ServerUrl
    • GenerateNoApiLink sub added for adding non api handler to documentation
    • ReadHandlers sub updated
    • JavaScript updated
      • fade-out and fade-in animation on bootstrap alert when submitting the endpoint again
      • html elements using id instead of button id
      • Some code for basic and token authentication support
      • Standardize coding format (removed ending semicolons and replaced single quote with double quotes)
  • help.html loads server url based on config.ini during production
Note: If you want to show help during Production, make sure run project in Debug mode at least once to generate help.html in Objects folder. Delete the old help.html and regenerate again if you have made new changes to the APIs. It is not recommend to enable the help page and expose it to public without protecting it.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Updates:
01/01/2025
  • Template:
    • Web API Server (3.20 beta 1).b4xtemplate
What's New:
  • Multi-line body format
  • Help can build by running in Debug or Release
  • Override auto-generated properties
  • Custom Method name (no longer mandatory to start with Verb e.g Get)
 

Attachments

  • Web API Server (3.20 beta1).b4xtemplate
    434.8 KB · Views: 85

aeric

Expert
Licensed User
Longtime User
Updates:
02/01/2025
  • Template:
    • Web API Server (3.20 beta 2).b4xtemplate
What's New:
  • This update is focus on Help (API documentation)
  • Multi-line json body format (for POST and PUT endpoints) - using String Literals, without using html
  • Help can be built programatically (Debug or Release)
  • Override most of the auto-generated properties if use together
  • Custom Method name (no longer mandatory to start with Verb e.g Get) but using hashtag comments e.g ' #Post - need to be added at right of sub definition
  • New Utility module to generate js files. No longer need to keep different js files for SimpleResponse as True and False
 

Attachments

  • Web API Server (3.20 beta2).b4xtemplate
    426.2 KB · Views: 83
Last edited:

aeric

Expert
Licensed User
Longtime User
Updates:
08/01/2025
  • Template:
    • Web API Server (3.20 beta3).b4xtemplate
What's New:
  • This update is focus on Help (API documentation)
  • Multi-line json body format (for POST and PUT endpoints) without using html - using String Literals or JSON Map
  • Help can be built programmatically (Debug or Release)
  • BuildMethods sub overrides the auto-generated properties if use together after calling ReadHandlers sub
  • Custom Method name (no longer mandatory to start with Verb e.g Get) but using hashtag comments e.g ' #Post - need to be added at right of sub definition
  • New Utility module to generate js files during Debug compilation
  • Update theme colour
  • Moved css and js in main.html to main.css and main.js)
version 3.20 beta3
 

Attachments

  • Web API Server (3.20 beta3).b4xtemplate
    427.3 KB · Views: 82
Last edited:

aeric

Expert
Licensed User
Longtime User
Updates:
09/01/2025
  • Template:
    • Web API Server (3.20 beta4).b4xtemplate
What's New:
  • This update is focus on Help (API documentation)
  • Multi-line json body format (for POST and PUT endpoints) without using html - using String Literals or JSON Map
  • Help can be built programmatically (Debug or Release)
  • BuildMethods sub overrides the auto-generated properties if use together after calling ReadHandlers sub
  • Custom Method name (not necessary to start with Verb but can use hashtag comments e.g ' #Post - same line with sub definition)
  • New Utility module to generate js files during Debug compilation
  • Moved css and js in main.html to main.css and main.js)
  • (update) Update theme colour
  • (update) Replace link in label for collapsible button with endpoint description
  • (update) Remove description in collapsible button details and move authentication indicator (pill) out
  • (update) Create JS files if not exist in release
version 3.20 beta4
 

Attachments

  • Web API Server (3.20 beta4).b4xtemplate
    427.6 KB · Views: 78

aeric

Expert
Licensed User
Longtime User
Updates:
10/01/2025
  • Template:
    • Web API Server (3.20 beta5).b4xtemplate
What's New:
  • This update is focus on Help (API documentation)
  • Multi-line json body format (for POST and PUT endpoints) without using html syntax- using String Literals or JSON Map
  • Help can be built programmatically (Debug or Release). ReadHandlers is no longer required.
  • BuildMethods sub can be use together with ReadHandlers sub to override generated properties.
  • Custom Method name (not necessary to start with Verb but can use hashtag comments e.g ' #POST - same line with sub definition)
  • New Utility module to generate JS files (Debug or Release)
  • Moved CSS and JS in main.html to main.css and main.js
  • Update theme colour
  • Replace link in label for collapsible button with endpoint description
  • Remove description in collapsible button details and move authentication indicator (pill) out
  • (update) Added Configurable sub for additional configuration.
  • (update) Some code clean up in Main module
  • (update) Remove comment hashtags for all Endpoint methods (ReadHandlers sub is no longer working without these comment hashtags)
  • (update) All endpoints documentation is now build using BuildMethods sub.
 

Attachments

  • Web API Server (3.20 beta5).b4xtemplate
    427.2 KB · Views: 85

aeric

Expert
Licensed User
Longtime User
Releases:
10/01/2025
This update is focus on API documentation (HelpHandler)
  • Template:
    • Web API Server (3.20).b4xtemplate
What's New:
  • Changed in UI and Theme
    • Moved CSS and JS in main.html to separate files (main.css and main.js)
    • Colour changed using custom CSS in main.css instead of Bootstrap variables
    • Replace label for collapsible button with endpoint description instead of showing the url link
    • Same for authentication indicator is put next to the description - showing Basic or Token when applicable
  • New BuildMethods sub is used to build API documentation programmatically
    • Now works when building project straight in Release mode
    • ReadHandlers sub is no longer required
    • However, BuildMethods sub can be use together with ReadHandlers sub to override its generated properties
    • All commented hashtags are now removed for a cleaner template
    • Note that commented hashtags are still working in Debug mode and can be used to generate static help.html for production in old ways
  • New commented hashtag (e.g ' #POST) *Note: all commented hashtags are case insensitive
    • Allow to use any name for naming Method sub in API handlers
    • Not necessary to start the Method name with a Verb e.g CreateNewCategory instead of PostCategory
    • The hashtag must be put on the same line with sub definition
  • Support for multi-line json body format (for POST and PUT endpoints)
    • (old way) HTML syntax in one line (i.e using <br> and &nbsp; tags)
    • (new) String Literals
    • (new) JSON Map *must be valid
  • New Utility module to generate JS files
    • Works during Debug or Release mode
    • It is executed by calling CreateJSFiles sub when InitServer is executed
    • The files are generated based on SimpleResponse settings
  • New Configurable sub for additional configuration
    • This helps to simplify a bit on configs in Main module
    • Developer only need to focus on 3 subs (in Main module) in most use cases
      • AppStart
      • Configurable
      • CreateDatabase
Tutorial will be updated soon.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Releases:
10/03/2025
This update is due to breaking changes of MiniORMUtils v2.00.
  • Template:
    • Web API Server (3.30).b4xtemplate
What's New:
  • SQLite version set to sqlite-jdbc-3.7.2
  • SQLite conditional symbol changed from server to WAL
  • MiniORMUtils method changes
  • Variables name renamed
  • Test database connection (MySQL)
 

aeric

Expert
Licensed User
Longtime User
Releases:
23/04/2025
  • Template:
    • Web API Server (3.40).b4xtemplate
What's New:
  • change code for initializing MiniORM object (support for MiniORMUtils v2.51)
  • update InitDatabase sub
  • rename DBEngine sub to DBType
  • fix support for custom api name in Utility module
  • clean unused code
  • remove unused items for SQLite in config.example
 

aeric

Expert
Licensed User
Longtime User
24/04/2025
  • Template:
    • Web API Server (3.42).b4xtemplate
What's New:
  • Fix for column names
  • Uncomment journal mode for SQLite
 
Top