Android Question Curl command to B4A Code

mike1967

Active Member
Licensed User
Longtime User
Hello, Can someone translate this curl command to b4a code:
curl command:
curl --request POST \
  --url http://siteurl:8080/ \
  --header 'content-type: application/json' \
  --data '{
  "url": "template.pdf",
  "values": {
    "topmostSubform[0].Page1[0].f1_1[0]": "Foo Bar",
    "topmostSubform[0].Page1[0].f1_2[0]": "Baz LLC"
   
  }
}'

Thanks in advanced
 
Last edited:

aeric

Expert
Licensed User
Longtime User
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    B4XPages.SetTitle(Me, "API Client")
    
    Dim values As Map
    values.Initialize
    values.Put("topmostSubform[0].Page1[0].f1_1[0]", "Foo Bar")
    values.Put("topmostSubform[0].Page1[0].f1_2[0]", "Baz LLC")
        
    Dim data As Map
    data.Initialize
    data.Put("url", "template.pdf")
    data.Put("values", values)
    
    CallAPI(data)
End Sub

Sub CallAPI (data As Map)
    Try
        Dim j As HttpJob
        j.Initialize("", Me)
        j.PostString("http://siteurl:8080", data.As(JSON).ToString)
        j.GetRequest.SetContentType("application/json")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log(j.GetString)
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException.Message)
    End Try
    j.Release
End Sub

 
Upvote 0

mike1967

Active Member
Licensed User
Longtime User
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    B4XPages.SetTitle(Me, "API Client")
   
    Dim values As Map
    values.Initialize
    values.Put("topmostSubform[0].Page1[0].f1_1[0]", "Foo Bar")
    values.Put("topmostSubform[0].Page1[0].f1_2[0]", "Baz LLC")
       
    Dim data As Map
    data.Initialize
    data.Put("url", "template.pdf")
    data.Put("values", values)
   
    CallAPI(data)
End Sub

Sub CallAPI (data As Map)
    Try
        Dim j As HttpJob
        j.Initialize("", Me)
        j.PostString("http://siteurl:8080", data.As(JSON).ToString)
        j.GetRequest.SetContentType("application/json")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log(j.GetString)
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException.Message)
    End Try
    j.Release
End Sub

Very Very Thanks,it's work fine. But the resulte of the request is a pdf files, how to save it to my phone ? Thanks in advances
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Very Very Thanks,it's work fine. But the resulte of the request is a pdf files, how to save it to my phone ? Thanks in advances
B4X:
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
       Dim out As OutputStream = File.OpenOutput(File.DirInternal, "filename.pdf", False)
     File.Copy2(j.GetInputStream, out)
     out.Close '<------ very important
   End If
   j.Release

Taken from the tutorial. I expect you have read it.

 
Upvote 2

mike1967

Active Member
Licensed User
Longtime User
B4X:
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
       Dim out As OutputStream = File.OpenOutput(File.DirInternal, "filename.pdf", False)
     File.Copy2(j.GetInputStream, out)
     out.Close '<------ very important
   End If
   j.Release

Taken from the tutorial. I expect you have read it.

Thanks
 
Upvote 0
Top