personalapps
Member
How do I convert this curl to b4x? I am trying to post the scribbling from b4x signature template and trying to get reply from Gemini:
This is my b4xcode:
The error I get is:
*** Receiver (httputils2service) Receive (first time) ***
tagSocket(104) with statsTag=0xffffffff, statsUid=-1
ResponseError. Reason: , Response: {
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"text\" at 'contents[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"type\" at 'contents[1]': Cannot find field.\nInvalid JSON payload received. Unknown name \"inline_data\" at 'contents[1]': Cannot find field.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "contents[0]",
"description": "Invalid JSON payload received. Unknown name \"text\" at 'contents[0]': Cannot find field."
},
{
"field": "contents[1]",
"description": "Invalid JSON payload received. Unknown name \"type\" at 'contents[1]': Cannot find field."
},
{
"field": "contents[1]",
"description": "Invalid JSON payload received. Unknown name \"inline_data\" at 'contents[1]': Cannot find field."
}
]
}
]
}
}
Error: {
[/QUOTE]
B4X:
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${GOOGLE_API_KEY} \
-H 'Content-Type: application/json' \
-d '{
"contents":[{
"parts":[
{"text": "What is this picture?"},
{"inline_data": {
"mime_type":"image/png",
"data": "'"$(base64 -i 'path/to/image/image.png')"'"
}}
]}
]
}'
B4X:
Sub SendSignatureToGemini
' Capture the signature as an image
Dim bmp As B4XBitmap = SignatureTemplate.Bitmap
' Convert the bitmap to a byte array
Dim ImageBytes() As Byte = ImageToBytes(bmp, "PNG")
If ImageBytes = Null Then
Log("Failed to convert image to bytes")
Return
End If
' Convert the byte array to a Base64 string
Dim Base64Image As String = su.EncodeBase64(ImageBytes)
' Prepare the JSON payload
Dim JSON As JSONGenerator
Dim partsList As List
partsList.Initialize
' Part 1: Text part
Dim part1 As Map
part1.Initialize
part1.Put("text", "What is this picture?")
partsList.Add(part1)
' Part 2: Inline data part
Dim part2 As Map
part2.Initialize
part2.Put("type", "inline_data")
Dim inline_data As Map
inline_data.Initialize
inline_data.Put("mime_type", "image/png")
inline_data.Put("data", Base64Image)
part2.Put("inline_data", inline_data)
partsList.Add(part2)
' Contents List
Dim contentsList As List
contentsList.Initialize
contentsList.Add(partsList)
Dim rootMap As Map
rootMap.Initialize
rootMap.Put("contents", contentsList)
JSON.Initialize(rootMap)
Dim JSONString As String = JSON.ToString
' Set up the HttpJob
Dim Job As HttpJob
Job.Initialize("GeminiJob", Me)
Job.PostString($"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${YOUR_GOOGLE_API_KEY}"$, JSONString)
Job.GetRequest.SetContentType("application/json")
End Sub
Sub JobDone(Job As HttpJob)
If Job.Success Then
Dim response As String = Job.GetString
Log("Response: " & response)
' Handle the response here
Else
Log("Error: " & Job.ErrorMessage)
End If
Job.Release
[QUOTE]
End Sub
*** Receiver (httputils2service) Receive (first time) ***
tagSocket(104) with statsTag=0xffffffff, statsUid=-1
ResponseError. Reason: , Response: {
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"text\" at 'contents[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"type\" at 'contents[1]': Cannot find field.\nInvalid JSON payload received. Unknown name \"inline_data\" at 'contents[1]': Cannot find field.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "contents[0]",
"description": "Invalid JSON payload received. Unknown name \"text\" at 'contents[0]': Cannot find field."
},
{
"field": "contents[1]",
"description": "Invalid JSON payload received. Unknown name \"type\" at 'contents[1]': Cannot find field."
},
{
"field": "contents[1]",
"description": "Invalid JSON payload received. Unknown name \"inline_data\" at 'contents[1]': Cannot find field."
}
]
}
]
}
}
Error: {
[/QUOTE]