Sending a ZIP file to a REST API using Delphi

  

I try to send a zip file to REST API using TRESTREQUEST (FMX Delphi Tokyo 10.2), the server responds correctly when I use PostMan utility tool. But when I use delphi code, the REST API doesn’t see the file in the zip archive sent.

The difference I have noticed between infos in postman and my code is:

1- In PostMan, “application/x-www-form-urlencoded” is the value for key “Content-Type” in header section.

2- After executing the request, the response’s content length is 2 with PostMan and 158 with delphi code.

PostMan request doing all things good (result code=200), but not the code bellow (result code=500):

procedure TForm1.SendFile3(AFileName: string);
var
ShortFName: string;
FileStream: TMemoryStream;
const
BASE_URL = ‘myRestAPIurl’;
RESSOURCE_NAME = ‘myRessourceName’;
begin
ShortFName := ExtractFileName(AFileName);
FileStream := TMemoryStream.Create;
try
FileStream.LoadFromFile(AFileName);
FileStream.Position := 0;

RESTClient3.BaseURL := BASE_URL;
RESTRequest3.Resource := RESSOURCE_NAME;
RESTRequest3.Method := rmPost;
RESTRequest3.Params.Clear;

RESTRequest3.Params.AddItem(‘fichier’, ShortFName, pkGETorPOST,
[], ctMULTIPART_FORM_DATA);
RESTRequest3.AddBody(FileStream, ctMULTIPART_FORM_DATA);

RESTRequest3.Execute;
finally
begin
FileStream.Free;
end;
end;

Memo1.Lines.Clear;

Memo1.Lines.Add(‘Response Code : ‘ +
RESTResponse3.StatusCode.ToString);
Memo1.Lines.Add(RESTResponse3.StatusText);
Memo1.Lines.Add(RESTResponse3.Content);

Memo1.Lines.Add(‘*****’);

Memo1.Lines.Add(‘Server: ‘ + RESTResponse3.Server);
Memo1.Lines.Add(‘Content: ‘ + RESTResponse3.Content);
Memo1.Lines.Add(‘Content tyle: ‘ + RESTResponse3.ContentType);
Memo1.Lines.Add(‘Content length: ‘ +
RESTResponse3.ContentLength.ToString);
end;

Any suggestions ?

Thanks!

Comments are closed.