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

Shopware Upload Media (JPG File)

See more Shopware Examples

Demonstrates how to upload a media file to a Shopware shop.

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.BinData,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  sbFileData: TStringBuilder;
  bdFileData: TBinData;
  json: TJsonObject;
  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;

  //  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"
  //  }

  sbFileData := TStringBuilder.Create;
  sbFileData.Append('data:image/jpeg;base64,');
  bdFileData := TBinData.Create;
  success := bdFileData.LoadFile('qa_data/jpg/starfish.jpg');
  if (success = False) then
    begin
      WriteLn('Failed to load file.');
      Exit;
    end;
  sbFileData.Append(bdFileData.GetEncoded('base64'));

  json := TJsonObject.Create;
  json.UpdateString('file',sbFileData.GetAsString());
  json.UpdateInt('album',-9);
  json.UpdateString('description','image description');
  json.UpdateInt('userId',5);
  json.UpdateString('name','my_image');

  resp := THttpResponse.Create;
  success := http.HttpJson('POST','https://my-shopware-shop.com/api/media',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 201 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": 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.BoolOf('success');
  dataId := jResp.IntOf('data.id');
  dataLocation := jResp.StringOf('data.location');


  http.Free;
  sbFileData.Free;
  bdFileData.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.