LuaPower/libcurl module examples.

libcurlのFFI bindingだったのでsample.

write call backの書き方

local function get_write_callback(f)
  return function(buf, sz, userdata)
    f:write(ffi.string(buf,sz))
    return sz
    -- memo: ここで消費バイト数を返し忘れると戻り先で nil を 整数変換しようとして落ちる。エラーが分かりにくいので注意 --
  end
end

local datafile = io.tmpfile()
curl_opt.writefunction = get_write_callback(datafile)

追加のHeaderの書き方

これは普通にこんな感じで書ける

curl_opt.request_header = {
  "Content-Type: application/json",
  "Accept: application/json",
  "X-Auth-Token: " .. auth_token
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.