Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Shopware Update Product Data

See more Shopware Examples

Update information about an existing product in Shopware.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.StringBuilder,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  json: TJsonObject;
  url: string;
  resp: THttpResponse;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  respStatusCode: Integer;
  dataId: Integer;
  dataLocation: string;

begin
  success := False;

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

  http := THttp.Create;

  http.Login := 'api_username';
  http.Password := 'api_key';
  http.BasicAuth := True;

  //  The following JSON is sent in the request body.

  //  {
  //    "name": "Super-Duper Sports Shoes"
  //  }

  json := TJsonObject.Create;
  json.UpdateString('name','Super-Duper Sports Shoes');

  //  The id of the product is appended to the path part of the URL.
  http.SetUrlVar('id','8312');

  url := 'https://my-shopware-shop.com/api/articles/{$id}';
  resp := THttpResponse.Create;
  success := http.HttpJson('PUT',url,json,'application/json',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  sbResponseBody := TStringBuilder.Create;
  resp.GetBodySb(sbResponseBody);

  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(jResp.Emit());

  //  A 200 response code indicates success.
  respStatusCode := resp.StatusCode;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(resp.Header);
      WriteLn('Failed.');
      Exit;
    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.BoolOf('success');
  dataId := jResp.IntOf('data.id');
  dataLocation := jResp.StringOf('data.location');


  http.Free;
  json.Free;
  resp.Free;
  sbResponseBody.Free;
  jResp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.