Android Code Snippet Conversion from CUrl to B4A

It is quite convenient to upload images to public cloud server and share the image link to other people rather than share the real image files directly!
The following code snippets showed how to upload and search images by CUrl command (provided by: https://apidocs.imgur.com/) and by B4A syntax.

Image upload to imgur by CUrl vs B4A

1. Search image in imgur by keyword:
[CODE title="search images by curl"]curl --location -g --request GET 'https://api.imgur.com/3/gallery/search?q=monkey' --header 'Authorization: Client-ID {{clientId}}'[/CODE]
[CODE lang="b4x" title="search images by B4A"]' Search keyword of image (i.e. anonymous get)
Public Sub sendGetRequest() As ResumableSub
Dim url2 As String = "https://api.imgur.com/3/gallery/search"
Dim auth As String = "Client-ID " & m_clientid '<-- your client id
Dim job As HttpJob
job.Initialize("get", Me)
job.Download2(url2, Array As String("q", "monkey"))
job.GetRequest.SetHeader("authorization", auth)
Wait For (job) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
Return True
End If
Return False
End Sub[/CODE]

2. Upload image anonymously to imgur:
[CODE title="image upload"]curl --location --request POST 'https://api.imgur.com/3/image' --header 'Authorization: Client-ID {{clientId}}' --form 'image="R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"'[/CODE]
[CODE lang="b4x" title="image upload by b4a"]' Anonymous upload
Public Sub sendPostRequest() As ResumableSub
Dim url As String = "https://api.imgur.com/3/image"
Dim auth As String = "Client-ID " & m_clientid '<-- your client id
Dim job As HttpJob
job.Initialize("post", Me)
Dim mapData As Map
mapData.Initialize
mapData.Put("image", "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")
mapData.Put("type", "base64")
'Dim jsonGen As JSONGenerator
'jsonGen.Initialize(mapData)
job.PostMultipart(url, mapData, Null)
job.GetRequest.SetHeader("Authorization", auth)
Wait For (job) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
Return True
End If
Return False
End Sub[/CODE]

3. Upload image to imgur account (i.e. non-anonymous):
[CODE title="image upload to account"]curl -X POST -H "Authorization: Bearer {{accessToken}}" -F "image=R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" https://api.imgur.com/3/upload[/CODE]
[CODE lang="b4x" title="image upload to account by b4a"]' Non-anonymous upload to user's account
Public Sub sendPostRequest2() As ResumableSub
Dim url As String = "https://api.imgur.com/3/upload"
Dim auth As String = "BEARER " & m_accesstoken '<-- your access token
Dim job As HttpJob
job.Initialize("post", Me)
Dim mapData As Map
mapData.Initialize
mapData.Put("image", "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")
mapData.Put("type", "base64")
job.PostMultipart(url, mapData, Null)
job.GetRequest.SetHeader("Authorization", auth)
Wait For (job) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
Return True
End If
Return False
End Sub[/CODE]
 

Attachments

Nice. You might be interested in this Wish:

 
Back
Top