B4J Question Curl to HttpJob

imbault

Well-Known Member
Licensed User
Longtime User
Are they both GET requests?

Have you tried follow the tutorial?

Try add the following after calling job.Download
B4X:
job.GetRequest.SetHeader("Authorization", "BASIC c2dzX2FkbWluOkxTZGJAZG1pbjAw")
 
Upvote 0
Hello,

Can someone give me the equivalent using HttpJob to those 2 Curl commands, please

1 - this one should return a JSon :
curl https://www.cloud.lims.fr/lims/webservices/databases
-H "Authorization: Basic c2dzX2FkbWluOkxTZGJAZG1pbjAw"


2 - this one should return a Binary file :
curl https://www.cloud.lims.fr/lims/webservices/databases/NUTRILABO-20231106-Monday.backup
-H "Authorization: Basic c2dzX2FkbWluOkxTZGJAZG1pbjAw"


Thanks a lot
1.
B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://www.cloud.lims.fr/lims/webservices/databases")
    j.GetRequest.SetHeader("Authorization","Basic c2dzX2FkbWluOkxTZGJAZG1pbjAw")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release
2.
B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://www.cloud.lims.fr/lims/webservices/databases/NUTRILABO-20231106-Monday.backup")
    j.GetRequest.SetHeader("Authorization","Basic c2dzX2FkbWluOkxTZGJAZG1pbjAw")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.DirApp, "filename.dat", False)
        File.Copy2(j.GetInputStream, out)
        out.Close
    End If
    j.Release
 
Upvote 0
Hello,

Can someone give me the equivalent using HttpJob to those 2 Curl commands, please

1 - this one should return a JSon :
curl https://www.cloud.lims.fr/lims/webservices/databases
-H "Authorization: Basic c2dzX2FkbWluOkxTZGJAZG1pbjAw"


2 - this one should return a Binary file :
curl https://www.cloud.lims.fr/lims/webservices/databases/NUTRILABO-20231106-Monday.backup
-H "Authorization: Basic c2dzX2FkbWluOkxTZGJAZG1pbjAw"


Thanks a lot

Here is simple example how use simple call - is api live:

B4X:
Sub IsAPILive As ResumableSub

    Dim Ok As Boolean = False
    ' Url is written in INI file which is readed on start
    Dim url As String = Main.InitConfig.Get("http_link") & "hallo"
    Dim requestBody As String = "" ' Modify this as per your requirements
   
    Dim lJobs As List
    lJobs.Initialize

     Dim Job As HttpJob
    Job.Initialize("isLive", Me)
    Job.PostString(url, requestBody)
    Job.GetRequest.SetContentType("application/json")
    Job.GetRequest.Timeout = Main.InitConfig.Get("api_timeout")
   
    ' Adding header - like DateFrom
    Dim headers As Map
    headers.Initialize
    headers.Put("DateFrom", DateFrom)
   
    For Each headerKey As String In headers.Keys
        job.GetRequest.SetHeader(headerKey, headers.Get(headerKey))
    Next

    DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss"
    Dim Datum As String = DateTime.Date(DateTime.Now)  
   
    ' waiting for answer
    Wait For (Job) JobDone(Job As HttpJob)
   
    If Job.Success Then
        Dim response As String = Job.GetString
        ' Log(response) ' Do somet    hing with the response

        Dim jRead As JSONParser
        jRead.Initialize(response)
        Dim jRoot As Map = jRead.NextObject
       
        Dim answare As Int = jRoot.Get("a")
        Dim success As String = jRoot.Get("s")
       
        ' if is ok
        If answare = "200" And success = "Ok" Then
            Ok = True
        End If

    Else
        Log("Error: " & Job.ErrorMessage)
    End If
   
    Job.Release
   
    Return Ok
   
End Sub
 
Upvote 0
Back
Top