B4J Question how to get data from a web page

linuxmentana

Member
Licensed User
Hello,
I'm trying to retrieve some values from a web application (python/flask) I made

I'm able to upload file from B4X to my server, but now I need to read a return value with a list of files from my server (apache/ubuntu)

Coud You please help me? I found in the forum ParameterMap I think is involved, but...

Andrea
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Hi! Do you mean communicate with your flask server? That can be done with httpjob


You may better use json as the format for communicating.

 
Upvote 0

linuxmentana

Member
Licensed User
Thank You Enrique !
I'll study JSON ;-)

Meanwhile, I tried with httpjob without success :-(

I'm building a B4X client for desktop file handling on my vps,
I can successfully upload files, but I need to send a text request (part of file name) and retrieve a list of files (my flask server return an array of strings)
If I could have a little code start, normally I can go ahead ;-)
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Can you show how you call the flask endpoint from curl or postman.

I should then be able to translate that into B4x code for you.
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
i will usually use this to comunicate with API REST applications

B4X:
    Dim j As HttpJob
        j.Initialize("",Me)
        j.PostString("https://" & Main.url & "/endpoint",$"
            {
                "id": "${group}",
                "files": [
                    "${file1}"
                   ,"${file2}"
                ]
            }       
        "$ _
        )
    
        Wait For JobDone(job As HttpJob)

        If job.Success Then
            If job.Response.StatusCode = 200 Then
                log("YaY")
            end if
        end if
 
Upvote 0

linuxmentana

Member
Licensed User
Hi Andrew,
thank You

my first use of postman ;-)
hope is what You mean

postman.jpg
postman.jpg
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
try this (not tested)

B4X:
private Sub getlist As ResumableSub
    Private job As HttpJob
    Private retn As List
  
    retn.Initialize
    job.Initialize("",Me)
    job.download("ecommerce.complab.cloud/trova/sar")
'    job.GetRequest.SetHeader("accept","application/json") you may need this.

  
    Wait For (job) JobDone(job As HttpJob)
  
    If job.Success Then
        Private retn As String = job.GetString
        Private jp As JSONParser
      
        Try
            jp.Initialize(job.GetString)
            retn = jp.NextArray         
        Catch
            Log("the return was not an array ",lastexception.message)
        End Try
    Else
        Log("there was an error of some sort" & job.ErrorMessage)     
    End If
  
    job.Release
    Return retn
  
End Sub
 
Upvote 0

linuxmentana

Member
Licensed User
Wonderfull Andrew! Thank You very much!
Now I can do a lot of things! :)))))

tested and working code:

private Sub getlist As ResumableSub
Private job As HttpJob
Private retn As List

retn.Initialize
job.Initialize("",Me)
job.download("http://ecommerce.complab.cloud/trova/sar")
job.GetRequest.SetHeader("accept","application/json")


Wait For (job) JobDone(job As HttpJob)

If job.Success Then
'Private retn As String = job.GetString
Private jp As JSONParser

Try
jp.Initialize(job.GetString)
retn = jp.NextArray
Catch
Log("the return was not an array " & LastException.message)
End Try
Else
Log("there was an error of some sort" & job.ErrorMessage)
End If

job.Release
Log(retn)
Return retn

End Sub
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
sample:

It has not indicated if you have an access token or username and password

B4X:
Public Sub TestGetSarApiURL
   
    '---- GET method types ---
   
    Dim BaseUrl As String = "https://my api web"
   
    'Option 1:  without parameters
    Wait For (GetSarApiURL1(BaseUrl)) Complete (Result As String)
    Log(Result)
   
    'Option 2: with Parameters
    Dim Parameters() As String = Array As String ("Parameter1", "Value 1", "Parameter2", "value 2")
    Wait For (GetSarApiURL2(BaseUrl, Parameters)) Complete (Result As String)
    Log(Result)
   
End Sub

Public Sub GetSarApiURL1(URL As String ) As ResumableSub
    Dim ResultURL As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download(URL)
        j.GetRequest.SetHeader("Content-Type","application/json") 'Opcional: if return JSON
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            ResultURL = j.GetString
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return ResultURL
End Sub

Public Sub GetSarApiURL2(URL As String, Parameters() As String) As ResumableSub
    Dim ResultURL As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download2(URL, Parameters)
        j.GetRequest.SetHeader("Content-Type","application/json") 'Opcional: if return JSON
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            ResultURL = j.GetString
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return ResultURL
End Sub

1625240522838.png
 
Upvote 0
Top