Base64 decoding error, when posting an encoded image to an API

Hello,

I am trying to POST a local image (photo2.jpg) to Google Vision API, but I am stuck with error message:

Invalid value at 'requests[0].image.content' (TYPE\_BYTES), Base64 decoding failed for \"..base64\""

The code is:

 -- loading the binary file local path2 = system.pathForFile( "photo2.jpg", system.DocumentsDirectory ) local file2 = io.open(path2,"rb") local temp = file2:read("\*a") io.close(file2) file2 = nil -- converting the binary stored in temp to base64 local function base64encode(data) local len = data:len() local t = {} for i=1,len,384 do local n = math.min(384, len+1-i) if n \> 0 then local s = data:sub(i, i+n-1) local enc, \_ = mime.b64(s) t[#t+1] = enc end end return table.concat(t) end local base64 = base64encode(temp) local function networkListener( event ) local res = json.prettify( event.response ) local decoded = json.decode( res ) if ( event.isError ) then print( "Network error: ", event.response ) else print( "Result: " .. ( res ) ) --This seems to display Base64 properly print(base64) end end --Somehow Base64 decoding failed! local headers = {} headers["Content-Type"] = "application/json" local body = '{"requests": [{"image": {"content": "..base64"},"features": [{"type": "TEXT\_DETECTION"}]}]}' local params = {} params.headers = headers params.body = body network.request( "https://vision.googleapis.com/v1/images:annotate?key="..apikey, "POST", networkListener, params )

I also tried the following simpler function (in place of base64encode function above):

local mime = require("mime") local result = mime.b64(temp)

It returns the same error.

In both cases, the Corona Simulator console seems to output Base64 data properly…

I look at documentation of the Google API and check Base64 related websites, but run out of ideas how to solve this. Do you have similar experience? Any hint?

Looks like you need to specify the body using Lua table like this:

[lua]

local body = {
   requests = {
      image = {
         content = “…”,
         features = {
            type = “TEXT_DETECTION”,
         },
      },
   },
}

[/lua]

sort out the string delimiters and concatenation in the assignment to “body” - you’re passing literally “…base64”, not its value

Thank you for quick help! @Dave Yang and @davebollinger

So, it was quite simple. I was not entirely sure how to insert a variable in Lua. After a few trials, only 1 line of code change works:

    local body = '{"requests": [{"image": {"content": "'..base64..'"},"features": [{"type": "TEXT\_DETECTION"}]}]}'

If I understand correctly (https://docs.coronalabs.com/api/library/network/request.html), body has to be string. So, my syntax works fine. Table should be used when it specifies a file as the content of the body.

Nevertheless, it was good to ask, as I am not sure if I could find a solution by myself. Cheers!

Looks like you need to specify the body using Lua table like this:

[lua]

local body = {
   requests = {
      image = {
         content = “…”,
         features = {
            type = “TEXT_DETECTION”,
         },
      },
   },
}

[/lua]

sort out the string delimiters and concatenation in the assignment to “body” - you’re passing literally “…base64”, not its value

Thank you for quick help! @Dave Yang and @davebollinger

So, it was quite simple. I was not entirely sure how to insert a variable in Lua. After a few trials, only 1 line of code change works:

    local body = '{"requests": [{"image": {"content": "'..base64..'"},"features": [{"type": "TEXT\_DETECTION"}]}]}'

If I understand correctly (https://docs.coronalabs.com/api/library/network/request.html), body has to be string. So, my syntax works fine. Table should be used when it specifies a file as the content of the body.

Nevertheless, it was good to ask, as I am not sure if I could find a solution by myself. Cheers!