Sample code for 30+ languages & platforms
Lua

MercadoLibre - Cargar factura (Upload Invoice)

See more MercadoLibre Examples

Cargar factura en detalle de venta

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local http = chilkat.Http.new()

-- First get our previously obtained OAuth2 access token.
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/mercadolibre.json")

-- Implements the following CURL command:

-- curl -X POST https://api.mercadolibre.com/packs/$PACK_ID/fiscal_documents?access_token=$ACCESS_TOKEN
--   -H 'content-type: multipart/form-data;'
--   -F 'fiscal_document=@/home/user/.../Factura_adjunta.pdf'

-- Use the following online tool to generate HTTP code from a CURL command
-- Convert a cURL Command to HTTP Source Code

local sbPath = chilkat.StringBuilder.new()
sbPath:append("/packs/$PACK_ID/fiscal_documents?access_token=$ACCESS_TOKEN")
-- Use an actual pack ID.
local numReplaced = sbPath:replace("$PACK_ID", "2000000089077943")
numReplaced = sbPath:replace("$ACCESS_TOKEN", jsonToken:string_of("access_token"))

local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = sbPath:get_as_string()
req.content_type = "multipart/form-data"
success = req:add_file_for_upload2("fiscal_document", "/home/user/.../Factura_adjunta.pdf", "application/pdf")

req:add_header("Expect", "100-continue")
req:add_header("content-type", "multipart/form-data;")

local resp = chilkat.HttpResponse.new()
success = http:http_sreq("api.mercadolibre.com", 443, true, req, resp)
if not success then
    print(http.last_error_text)
    return
end

local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false

print("Response Body:")
print(jResp:emit())

local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
    print("Response Header:")
    print(resp.header)
    print("Failed.")
    return
end

-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)

-- {
--   "ids": [
--     "415460047_a96d8dea-38cd-4402-938e-80a1c134fc5d"
--   ]
-- }

-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON

local strVal = ""

local i = 0
local count_i = jResp:size_of_array("ids")
while i < count_i do
    jResp.i = i
    strVal = jResp:string_of("ids[i]")
    i = i + 1
end