Wrk POST 请求中, 图片转 base64 的工具函数无法调用是怎么回事? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
css3

Wrk POST 请求中, 图片转 base64 的工具函数无法调用是怎么回事?

  •  
  •   css3 2020 年 11 月 16 日 1283 次点击
    这是一个创建于 1987 天前的主题,其中的信息可能已经有所发展或是发生改变。

    wrk发起post请求,request body中有一个value要求传入图片的base64编码,网上找了一个图片转base64lua 脚本,导入后,能够把图片转成base64,但wrk无法调用?把函数当成字符串了, 因为body传入的时候,本身就是字符串格式,怀疑把函数直接放进去当成字符串了,尝试把body`进行字符串加变量拼接,也没用,非常的尴尬啊,老哥们帮忙看看

    -- post.lua require("ZZBase64") local files local img = "test.jpg" local file = io.open(img,"rb") files = file:read("*a") file:close() -- print(ZZBase64.encode(files)) 能够打印出正常编码后的值 wrk.method = "POST" wrk.headers["Content-Type"] = "application/json" wrk.body = '{ "data": [ { "image": { "base64": ZZBse64.encode(files) } }] }' respOnse= function(status, headers, body) print(status) end -- 接口返回 400, 提示 request 不正确,并且把 request 打印了出来,ZZBase64.encode(files)原样在里边 

    Ctrl V 的 image to base64 lua

    -- ZZBase64.lua ZZBase64 = {} local string = string ZZBase64.__code = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', }; ZZBase64.__decode = {} for k,v in pairs(ZZBase64.__code) do ZZBase64.__decode[string.byte(v,1)] = k - 1 end function ZZBase64.encode(text) local len = string.len(text) local left = len % 3 len = len - left local res = {} local index = 1 for i = 1, len, 3 do local a = string.byte(text, i ) local b = string.byte(text, i + 1) local c = string.byte(text, i + 2) -- num = a<<16 + b<<8 + c local num = a * 65536 + b * 256 + c for j = 1, 4 do --tmp = num >> ((4 -j) * 6) local tmp = math.floor(num / (2 ^ ((4-j) * 6))) --curPos = tmp&0x3f local curPos = tmp % 64 + 1 res[index] = ZZBase64.__code[curPos] index = index + 1 end end if left == 1 then ZZBase64.__left1(res, index, text, len) elseif left == 2 then ZZBase64.__left2(res, index, text, len) end return table.concat(res) end function ZZBase64.__left2(res, index, text, len) local num1 = string.byte(text, len + 1) num1 = num1 * 1024 --lshift 10 local num2 = string.byte(text, len + 2) num2 = num2 * 4 --lshift 2 local num = num1 + num2 local tmp1 = math.floor(num / 4096) --rShift 12 local curPos = tmp1 % 64 + 1 res[index] = ZZBase64.__code[curPos] local tmp2 = math.floor(num / 64) curPos = tmp2 % 64 + 1 res[index + 1] = ZZBase64.__code[curPos] curPos = num % 64 + 1 res[index + 2] = ZZBase64.__code[curPos] res[index + 3] = "=" end function ZZBase64.__left1(res, index,text, len) local num = string.byte(text, len + 1) num = num * 16 tmp = math.floor(num / 64) local curPos = tmp % 64 + 1 res[index ] = ZZBase64.__code[curPos] curPos = num % 64 + 1 res[index + 1] = ZZBase64.__code[curPos] res[index + 2] = "=" res[index + 3] = "=" end function ZZBase64.decode(text) local len = string.len(text) local left = 0 if string.sub(text, len - 1) == "==" then left = 2 len = len - 4 elseif string.sub(text, len) == "=" then left = 1 len = len - 4 end local res = {} local index = 1 local decode = ZZBase64.__decode for i =1, len, 4 do local a = decode[string.byte(text,i )] local b = decode[string.byte(text,i + 1)] local c = decode[string.byte(text,i + 2)] local d = decode[string.byte(text,i + 3)] --num = a<<18 + b<<12 + c<<6 + d local num = a * 262144 + b * 4096 + c * 64 + d local e = string.char(num % 256) num = math.floor(num / 256) local f = string.char(num % 256) num = math.floor(num / 256) res[index ] = string.char(num % 256) res[index + 1] = f res[index + 2] = e index = index + 3 end if left == 1 then ZZBase64.__decodeLeft1(res, index, text, len) elseif left == 2 then ZZBase64.__decodeLeft2(res, index, text, len) end return table.concat(res) end function ZZBase64.__decodeLeft1(res, index, text, len) local decode = ZZBase64.__decode local a = decode[string.byte(text, len + 1)] local b = decode[string.byte(text, len + 2)] local c = decode[string.byte(text, len + 3)] local num = a * 4096 + b * 64 + c local num1 = math.floor(num / 1024) % 256 local num2 = math.floor(num / 4) % 256 res[index] = string.char(num1) res[index + 1] = string.char(num2) end function ZZBase64.__decodeLeft2(res, index, text, len) local decode = ZZBase64.__decode local a = decode[string.byte(text, len + 1)] local b = decode[string.byte(text, len + 2)] local num = a * 64 + b num = math.floor(num / 16) res[index] = string.char(num) end 
    5 条回复    2020-11-17 18:48:25 +08:00
    6IbA2bj5ip3tK49j
        1
    6IbA2bj5ip3tK49j  
       2020 年 11 月 17 日
    wrk.body = '{"data":[{"image":{"base64": "'...ZZBase64.encode(files)...'"}}]}'
    css3
        2
    css3  
    OP
       2020 年 11 月 17 日
    @xgfan 老哥,也不行呐,报错 attempt to call a string value
    css3
        3
    css3  
    OP
       2020 年 11 月 17 日
    6IbA2bj5ip3tK49j
        4
    6IbA2bj5ip3tK49j  
       2020 年 11 月 17 日
    @css3 那可能是我记错了……
    css3
        5
    css3  
    OP
       2020 年 11 月 17 日 via iPhone
    @xgfan 我也是第一次写 lua,头一次遇到用点拼接的
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2858 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 12:45 PVG 20:45 LAX 05:45 JFK 08:45
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86