B4J Tutorial Pocketbase CRUD REST API & SSE with PostMan and then HttpUtils

Hi

CRUD Examples Attached

PocketBase
is an open source backend consisting of embedded database (SQLite) with realtime subscriptions, built-in users management, convenient dashboard UI and simple REST-ish API.



It runs on Linux, Windows and Mac, You can get it here, https://pocketbase.io/

We will look at each offering using HTTPUtils, thus covering the REST API both with postman & b4x.

Here we go.

PS: Do not forget to start the server

1668731707741.png
 

Attachments

  • PocketBase.zip
    6.9 KB · Views: 262
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Lesson 1: Creating a User for Email Authentication...

PocketBase has various auth providers. These are...

1668731265735.png


These can be set from its dashboard.

With PostMan, you can execute this call to create a user. Its a JSON Post request.


1668731228931.png


With B4x, I have created this simple example.

1668732541269.png


We have a class that will do some pocketbase things...

We initialize this:

B4X:
pb.Initialize(Me, "http://127.0.0.1:8090/api")

We then add our user

B4X:
Private Sub btnCreateUser_Click
    Dim u As Map = CreateMap()
    u.Put("email", txtEmail.text)
    u.Put("password", txtpassword.Text)
    u.Put("passwordConfirm", txtConfirmPassword.Text)
    Wait For(pb.CreateUser(u)) Complete (Result As Map)
    Dim pjson As String = pb.Map2JsonPretty(Result)
    txtOutput.Text = pjson
End Sub

This calls the internal function to the jPocketBase class...

B4X:
'<code>
'Dim u As Map = CreateMap()
'u.Put("email", txtEmail.text)
'u.Put("password", txtpassword.Text)
'u.Put("passwordConfirm", txtConfirmPassword.Text)
'Wait For(pb.CreateUser(u)) Complete (Result As Map)
'Log(Result)
'</code>
Sub CreateUser(u As Map) As ResumableSub
    Dim out As Map = CreateMap()
    'convert map
    Dim qs As String = Map2QueryString(u)
    '
    Dim j As HttpJob
    j.Initialize("", mcallback)
    j.PostString($"${baseUrl}/users"$, qs)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        out = Json2Map(j.GetString)
    Else 
        out = Json2Map(j.ErrorMessage)
    End If
    j.Release
    Return out
End Sub

In you dashboard, select the users button to see added users

1668732788575.png


1668732830829.png
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Remember the rest api needs to be fed something, so you if you are just opening that via a browser/postman without parameters, it does nothing, thus that response.

Also http://127.0.0.1:8090/api/ is the base endpoint. As an example, create a collection for example, call it projects, add some fields to it.

1668872143103.png

After the collection is added, select


1668872184142.png


This will give you examples of the API calls you need to append to the base url. See get below.


1668872212453.png


At the bottom of each tab it gives you the parameters that you need to pass

1668872287848.png


In the example above for example

B4X:
j.PostString($"${baseUrl}/users"$, qs)

I am using the base url and then giving it parameters via the map object I did. See also the postman example I did above.

In my example users is not a collection as its build internally to PocketBase.
 
Last edited:

thinktank

Member
Hello brother,

I have problem while executing your B4J example code, with postman the post request works fine and record is created in collection, however, with B4J example it was giving me following errors:

Post request error:
{
    "code": 404,
    "data": {},
    "message": "Not Found."
}

Second error I faced was :

2nd Error:
{
    "code": 400,
    "data": {},
    "message": "Failed to load the submitted data due to invalid formatting."
}

Which I fixed by changing your code in jPocketBase class -> CreateUser subroutine to following:

Posting Problem fixed:
    Dim qs As String = Map2Json(u)
   
    Dim j As HttpJob
    j.Initialize("pb", Me)
 j.PostString($"${baseUrl}/collections/users/records"$, qs)
 j.GetRequest.SetContentType("application/json")

This might help someone else, btw I am using latest pocketbase version 8.

BR
 

Mashiane

Expert
Licensed User
Longtime User
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Upgrading to Version 0.8 : Breaking Changes

1. Stop pocketbase

In the command prompt... execute.

2. cp -r ./pb_data ./pb_data_backup

3. Download new version and then unzip and copy it to c:\pocketbase etc.


4. Execute ./pocketbase upgrade

5. Execute ./pocketbase serve

You should see... on bottom ight section of PB dashboard

1669735081053.png
 
Last edited:

amorosik

Expert
Licensed User
Upgrading to Version 0.8 : Breaking Changes

1. Stop pocketbase

In the command prompt... execute.

2. cp -r ./pb_data ./pb_data_backup

3. Download new version and then unzip and copy it to c:\pocketbase etc.


4. Execute ./pocketbase upgrade

5. Execute ./pocketbase serve

You should see... on bottom ight section of PB dashboard

View attachment 136577


Why 0.8 version ???
On web site is available the 0.9 version
 

Mashiane

Expert
Licensed User
Longtime User
Flyio Hosting


Example



flyctl launch --dockerfile mashy.dockerfile --name sithasodaisy --region jnb

flyctl volumes create pb_data --size=1 --region jnb
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Thanks to Erels example here, I've just managed to get this working.


Source code on first post updated and now includes SSE.


 
Top