{"id":200112,"date":"2020-11-15T09:46:25","date_gmt":"2020-11-15T01:46:25","guid":{"rendered":"http:\/\/4563.org\/?p=200112"},"modified":"2020-11-15T09:46:25","modified_gmt":"2020-11-15T01:46:25","slug":"wrk-post-%e8%af%b7%e6%b1%82%e4%b8%ad%ef%bc%8c-%e5%9b%be%e7%89%87%e8%bd%ac-base64-%e7%9a%84%e5%b7%a5%e5%85%b7%e5%87%bd%e6%95%b0%e6%97%a0%e6%b3%95%e8%b0%83%e7%94%a8%e6%98%af%e6%80%8e%e4%b9%88%e5%9b%9e","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=200112","title":{"rendered":"Wrk POST \u8bf7\u6c42\u4e2d\uff0c \u56fe\u7247\u8f6c base64 \u7684\u5de5\u5177\u51fd\u6570\u65e0\u6cd5\u8c03\u7528\u662f\u600e\u4e48\u56de\u4e8b\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Wrk POST \u8bf7\u6c42\u4e2d\uff0c \u56fe\u7247\u8f6c base64 \u7684\u5de5\u5177\u51fd\u6570\u65e0\u6cd5\u8c03\u7528\u662f\u600e\u4e48\u56de\u4e8b\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : css3 <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p><code>wrk<\/code>\u53d1\u8d77<code>post<\/code>\u8bf7\u6c42\uff0c<code>request body<\/code>\u4e2d\u6709\u4e00\u4e2a<code>value<\/code>\u8981\u6c42\u4f20\u5165\u56fe\u7247\u7684<code>base64<\/code>\u7f16\u7801\uff0c\u7f51\u4e0a\u627e\u4e86\u4e00\u4e2a\u56fe\u7247\u8f6c<code>base64<\/code>\u7684<code>lua \u811a\u672c\uff0c\u5bfc\u5165\u540e\uff0c\u80fd\u591f\u628a\u56fe\u7247\u8f6c\u6210<\/code>base64<code>\uff0c\u4f46<\/code>wrk<code>\u65e0\u6cd5\u8c03\u7528\uff1f\u628a\u51fd\u6570\u5f53\u6210\u5b57\u7b26\u4e32\u4e86\uff0c \u56e0\u4e3a<\/code>body<code>\u4f20\u5165\u7684\u65f6\u5019\uff0c\u672c\u8eab\u5c31\u662f\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u6000\u7591\u628a\u51fd\u6570\u76f4\u63a5\u653e\u8fdb\u53bb\u5f53\u6210\u5b57\u7b26\u4e32\u4e86\uff0c\u5c1d\u8bd5\u628a<\/code>body`\u8fdb\u884c\u5b57\u7b26\u4e32\u52a0\u53d8\u91cf\u62fc\u63a5\uff0c\u4e5f\u6ca1\u7528\uff0c\u975e\u5e38\u7684\u5c34\u5c2c\u554a\uff0c\u8001\u54e5\u4eec\u5e2e\u5fd9\u770b\u770b<\/p>\n<pre><code>-- 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)) \u80fd\u591f\u6253\u5370\u51fa\u6b63\u5e38\u7f16\u7801\u540e\u7684\u503c  wrk.method = \"POST\" wrk.headers[\"Content-Type\"] = \"application\/json\" wrk.body = '{     \"data\": [     {         \"image\":         {             \"base64\": ZZBase64.encode(files)         }     }] }'  response = function(status, headers, body)        print(status) end  -- \u63a5\u53e3\u8fd4\u56de 400\uff0c \u63d0\u793a request \u4e0d\u6b63\u786e\uff0c\u5e76\u4e14\u628a request \u6253\u5370\u4e86\u51fa\u6765\uff0cZZBase64.encode(files)\u539f\u6837\u5728\u91cc\u8fb9 <\/code><\/pre>\n<p>Ctrl V \u7684 image to base64 lua<\/p>\n<pre><code>-- 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&lt;&lt;16 + b&lt;&lt;8 + c         local num = a * 65536 + b * 256 + c         for j = 1, 4 do             --tmp = num &gt;&gt; ((4 -j) * 6)             local tmp = math.floor(num \/ (2 ^ ((4-j) * 6)))             --curPos = tmp&amp;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&lt;&lt;18 + b&lt;&lt;12 + c&lt;&lt;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  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4213037\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xgfan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             wrk.body = &#8216;{&#8220;data&#8221;:[{&#8220;image&#8221;:{&#8220;base64&#8221;: &#8220;&#8216;&#8230;ZZBase64.encode(files)&#8230;'&#8221;}}]}&#8217;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4213038\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : css3 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xgfan \u8001\u54e5\uff0c\u4e5f\u4e0d\u884c\u5450\uff0c\u62a5\u9519 attempt to call a string value                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4213039\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : css3 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xgfan <br \/>https:\/\/stackoverflow.com\/questions\/1405583\/concatenation-of-strings-in-lua<br \/>\u7528\u4e86 2 \u4e2a\u70b9\uff0c\u53ef\u4ee5\u4e86\uff0c\u611f\u8c22\u8001\u54e5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4213040\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xgfan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @css3 \u90a3\u53ef\u80fd\u662f\u6211\u8bb0\u9519\u4e86\u2026\u2026                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4213041\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : css3 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xgfan                                                             <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Wrk POST \u8bf7\u6c42\u4e2d\uff0c \u56fe\u7247\u8f6c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/200112"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=200112"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/200112\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200112"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}