Lua
Lua
Shopware Create a New Product
See more Shopware Examples
Create a new product in Shopware.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.
-- {
-- "name": "Sport Shoes",
-- "active": true,
-- "tax": 19,
-- "supplier": "Sport Shoes Inc.",
-- "categories": [
-- {
-- "id": 384
-- }
-- ],
-- "mainDetail": {
-- "number": "turn",
-- "active": true,
-- "prices": [
-- {
-- "customerGroupKey": "EK",
-- "price": 999
-- }
-- ]
-- }
-- }
local json = chilkat.JsonObject.new()
json:update_string("name", "Sport Shoes")
json:update_bool("active", true)
json:update_int("tax", 19)
json:update_string("supplier", "Sport Shoes Inc.")
json:update_int("categories[0].id", 384)
json:update_string("mainDetail.number", "turn")
json:update_bool("mainDetail.active", true)
json:update_string("mainDetail.prices[0].customerGroupKey", "EK")
json:update_int("mainDetail.prices[0].price", 999)
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "https://my-shopware-shop.com/api/articles", 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": 8312,
-- "location": "https:\/\/my-shopware-shop.com\/api\/articles\/8312"
-- }
-- }
-- 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")