B4J Question PAKAI = WebApiUtils

Davisbc

Member
Licensed User
From one of the responces

You can find the following code on top of WebApiUtils.bas file.

Where can i find a copy to make the suggested edits to make the json easier to read?

Thanks in advance,
 

aeric

Expert
Licensed User
Longtime User
You can find the following code on top of WebApiUtils.bas file.
Edit: You mean the code inside Sub Process_Globals as changed in Version: 4.30
B4X:
    Public Const RESPONSE_ELEMENT_MESSAGE As String  = "m"
    Public Const RESPONSE_ELEMENT_CODE As String     = "a"
    Public Const RESPONSE_ELEMENT_STATUS As String   = "s"
    Public Const RESPONSE_ELEMENT_TYPE As String     = "t"
    Public Const RESPONSE_ELEMENT_ERROR As String    = "e"
    Public Const RESPONSE_ELEMENT_RESULT As String   = "r"

Where can i find a copy to make the suggested edits to make the json easier to read?
Do you mean to read the
1. JSON payload sent from the client to ther server using OkHttpUtils2 in Post/Put body
or
2. JSON responses that will send back to the client as output?
Edit: So now I think you are referring to the JSON response.

Can you post some code of what you are trying to achieve?
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Where can i find a copy to make the suggested edits to make the json easier to read?
Do you mean you want a simpler format with less keys?

Let me take an example for this endpoint
http://127.0.0.1:8080/api/products/3

The default response is:
JSON:
{
  "s": "ok",
  "a": 200,
  "m": "Success",
  "e": null,
  "r": {
    "id": 3,
    "category_id": 2,
    "product_code": "T002",
    "product_name": "Optimus Prime",
    "product_price": 1000,
    "product_image": null,
    "created_date": "2025-10-27 16:51:43",
    "modified_date": null,
    "deleted_date": null
  }
}
The default JSON contains keys "s", "a", "m", "e", "r".

Let say we only interested on the value for "r" key.
We expect the response as:
JSON:
{
  "id": 3,
  "category_id": 2,
  "product_code": "T002",
  "product_name": "Optimus Prime",
  "product_price": 1000,
  "product_image": null,
  "created_date": "2025-10-27 16:51:43",
  "modified_date": null,
  "deleted_date": null
}
This is easy in Pakai v5. By default, EndsMeet has set VerboseMode to True.
Just set the VerboseMode to False in AppStart in Main module.
B4X:
' *** Api Settings ***
Api = App.api
Api.VerboseMode = False
If you are debugging in Help, remember to Refresh the page to reload the JavaScript.

In Pakai v4, using WebApiUtils v4.50+ you need to comment the following line.
B4X:
Private Sub Configurable
    #If DEBUG
    conf.EnableHelp = True
    #End If
    #If RELEASE
    conf.EnableSSL = True
    #End If
    conf.EnableCORS = True
    'conf.VerboseMode = True
    conf.StaticFilesBrowsable = False
End Sub
 
Last edited:
Upvote 0

Davisbc

Member
Licensed User
In your earlier dialog, you showed 2 json results side by side. One using the default keys and the other using more descriptive keys. the suggestion was that by changing the globals in the WebApiUtils.bas file, you can make the keys more descriptive (https://www.b4x.com/android/forum/threads/web-webapiutils-v5-50.167012/#post-1024681).

My question is how do i update the WebApiUtils.bas source file to add the updated constants?

Thanks,
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
how do i update the WebApiUtils.bas
You don't need to modify the library.

Take the example of the screenshot for the following endpoint in FindApiHandler:
http://127.0.0.1:8080/api/find/products-by-category_id/2
You just need to add the following code:

For Pakai v4:
B4X:
Public Sub Initialize
    HRM.Initialize
    HRM.VerboseMode = Main.conf.VerboseMode
    HRM.ResponseKeys = Array("m", "a", "r")
    HRM.ResponseKeysAlias = Array("message", "code", "data")
End Sub

For Pakai v5:
B4X:
Public Sub Initialize
    App = Main.App
    HRM.Initialize
    Main.SetApiMessage(HRM)
    DB.Initialize(Main.DBType, Null)
    HRM.ResponseKeys = Array("m", "a", "r")
    HRM.ResponseKeysAlias = Array("message", "code", "data")
End Sub
 
Upvote 0
Top