Android Question Type image path in base64 format

khwarizmi

Active Member
Licensed User
Longtime User
Hi all

in the following code I want to type the image path in Base64 format, How can I do that :

B4X:
File.Copy(File.DirAssets,"t.jpg",File.DirInternal,"t.jpg")
    
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"t.jpg")
    Sleep(300)
    Dim job1 As HttpJob
    job1.Initialize("",Me)
    Log(base)
    job1.Download("https://api.luxand.cloud/photo/verify/30417? \")
    job1.GetRequest.SetHeader("token","-------")
    job1.GetRequest.SetHeader("photo",base) ' Here is the problem
    Wait For (job1) JobDone(job1 As HttpJob)
    ProgressDialogHide
    Dim res As String
    res = job1.GetString
        Log(res)

the image path has to be either URL or Base64
 

OliverA

Expert
Licensed User
Longtime User
job1.GetRequest.SetHeader("photo",base) ' Here is the problem
Looks to me like you're stuffing the whole BASE64 encoded picture into the header. I'm pretty sure that is wrong. Where is the API docs for the service you are using?
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
It is a service to compare the face image, here is the sample code page: https://dashboard.luxand.cloud/documentation/photo/verify

here is the curl code for that, the image has to be either URL or Base64 :

B4X:
curl https://api.luxand.cloud/photo/verify/30417? \
-X POST \
-H "token: ----------" \
-F "photo=@photo.jpg"
Perform request
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
-F "photo=@photo.jpg"
-F is not -H. -F implies multipart form. You probably need something like this
B4X:
File.Copy(File.DirAssets,"t.jpg",File.DirInternal,"t.jpg")
Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"t.jpg")

Dim posturl As String = "https://api.luxand.cloud/photo/verify/30417?" 'Note: I don't know why you have a ? at the end of the number.
'In a previous post (https://www.b4x.com/android/forum/threads/curl-request.116085/) that is not there. It's up to you to ensure
'that this URL is correct

Dim j As HttpJob
j.Initialize("", Me)
j.Post(posturl, $"photo=${base}"$)
j.GetRequest.SetHeader("token", "367802613a2f49c7b1e263aa09053bc0") ' or whatever you need to set. Do it here
j.GetRequest.SetContentType("application/x-www-form-urlencoded")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
     Log($"Success: ${j.GetString}"$)
Else
    Log($"Error: ${j.ErrorMessage}"$)
End If
j.Release
Note: Not tested.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
thanks so much .. I will test it and show you.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
B4X:
File.Copy(File.DirAssets,"t.jpg",File.DirInternal,"t.jpg")
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"t.jpg")
    Dim posturl As String = "https://api.luxand.cloud/photo/verify/30417? \" 'Note: I don't know why you have a ? at the end of the number.
' in the initial code in the page there is (? \) at the end.
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString(posturl, $"photo=${base}"$)
    j.GetRequest.SetHeader("token", "367802613a2f49c7b1e263aa09053bc0") ' or whatever you need to set. Do it here
    j.GetRequest.SetContentType("application/x-www-form-urlencoded")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log($"Success: ${j.GetString}"$)
    Else
        Log($"Error: ${j.ErrorMessage}"$)
    End If
    j.Release

but I get: ResponseError. Reason: INTERNAL SERVER ERROR, Response: {"message": "Internal Server Error"}
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
@kattah what happened if you do the program steps from your Browser? Such INTERNAL SERVER ERROR, Response: {"message": "Internal Server Error"} could perhaps given for a bad authorization (wrong url and/of key) ? Security wise there is something wrong, in that case it is not a bad password error) so IT guys must take an alternative for it.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Try
B4X:
File.Copy(File.DirAssets,"t.jpg",File.DirInternal,"t.jpg")

Dim posturl As String = "https://api.luxand.cloud/photo/verify/30417?" 'Note: I don't know why you have a ? at the end of the number.
'In a previous post (https://www.b4x.com/android/forum/threads/curl-request.116085/) that is not there. It's up to you to ensure
'that this URL is correct

Dim j As HttpJob
j.Initialize("", Me)
Dim fd As MultipartFileData
fd.Initialize
fd.KeyName = "photo" '<- this becomes the photo= part
fd.Dir = File.DirInternal
fd.FileName = "t.jpg" '<- make sure it's the right file
fd.ContentType = "image/jpg"
j.PostMultipart(posturl, Null, Array(fd))
j.GetRequest.SetHeader("token", "367802613a2f49c7b1e263aa09053bc0")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
     Log($"Success: ${j.GetString}"$)
Else
    Log($"Error: ${j.ErrorMessage}"$)
End If
j.Release

Source (by @DonManfred): https://www.b4x.com/android/forum/t...arameters-in-postmultipart.108385/post-677400
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…