Lua
Lua
Shopware Upload Media (JPG File)
See more Shopware Examples
Demonstrates how to upload a media file to a Shopware shop.Chilkat Lua Downloads
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()
http.login = "api_username"
http.password = "api_key"
http.basic_auth = true
-- Use this online tool to generate code from sample JSON:
-- Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "file": "data:image/jpeg;base64,BASE64_DATA_HERE",
-- "album": -9,
-- "description": "image description",
-- "userId": 5,
-- "name": "my_image"
-- }
local sbFileData = chilkat.StringBuilder.new()
sbFileData:append("data:image/jpeg;base64,")
local bdFileData = chilkat.BinData.new()
local success = bdFileData:load_file("qa_data/jpg/starfish.jpg")
if not success then
print("Failed to load file.")
return
end
sbFileData:append(bdFileData:get_encoded("base64"))
local json = chilkat.JsonObject.new()
json:update_string("file", sbFileData:get_as_string())
json:update_int("album", -9)
json:update_string("description", "image description")
json:update_int("userId", 5)
json:update_string("name", "my_image")
local resp = chilkat.HttpResponse.new()
success = http:http_json("POST", "https://my-shopware-shop.com/api/media", json, "application/json", 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())
-- A 201 response code indicates success.
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)
-- {
-- "success": true,
-- "data": {
-- "id": 6862,
-- "location": "http:\/\/my-shopware-shop.com\/api\/media\/6862"
-- }
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
success = jResp:bool_of("success")
local dataId = jResp:int_of("data.id")
local dataLocation = jResp:string_of("data.location")