Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Base64 decoding error, when posting an encoded image to an API
Started by GO4IT Jun 11 2019 05:26 AM

3 replies to this topic
base64 api google vision api post http request image

Best Answer davebollinger , 11 June 2019 - 10:16 AM

sort out the string delimiters and concatenation in the assignment to "body" - you're passing literally "..base64", not its value

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

GO4IT

[GLOBAL: userInfoPane.html]
GO4IT
  • Observer

  • 15 posts
  • Corona SDK

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?



[TOPIC: post.html]
#2

Quantumwave

[GLOBAL: userInfoPane.html]
Quantumwave
  • Contributor

  • 103 posts
  • Corona SDK

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

 

local body = {
   requests = {
      image = {
         content = "...",
         features = {
            type = "TEXT_DETECTION",
         },
      },
   },
}

 



[TOPIC: post.html]
#3

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,373 posts
  • Corona SDK

  Best Answer

sort out the string delimiters and concatenation in the assignment to "body" - you're passing literally "..base64", not its value



[TOPIC: post.html]
#4

GO4IT

[GLOBAL: userInfoPane.html]
GO4IT
  • Observer

  • 15 posts
  • Corona SDK

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!




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: base64, api, google vision api, post, http request, image