0

I try to emulate the same behavior like this console call:

# curl -X POST -F"file=@myfile.csv" 'https://myserver/upload'

Here is my Delphi 10.1 code:

procedure TForm1.Button1Click(Sender: TObject);
var localfile:String;
    localpath:string;
begin
  localfile := 'myfile.csv';
  localpath := 'C:\upload';

  SetCurrentDir(localpath);

  RESTClient.ContentType:= 'multipart/form-data';
  RESTClient.BaseURL:= 'https://myserver';
  RESTRequest.Accept:= '*/*';
  RESTRequest.Method:= rmPOST;
  RESTRequest.Resource:= 'upload';

  RESTRequest.AddParameter('file',localfile, TRESTRequestParameterKind.pkREQUESTBODY);
  RESTRequest.Execute;

  Memo1.Lines.Add(RESTResponse.Content);
end;

it fails with: 415 Unsupported Media Type

Ole
  • 1
  • 2
  • The curl command line you show don't do a REST request, it is a simple HTTP "POST" used to upload a file. You should use an HTTP client component. There are a lot available (I'm the author of ICS: http://wiki.overbyte.be). Your question is probably answered there: https://stackoverflow.com/questions/10889511/how-do-i-upload-a-file-using-http-post-delphi-2009 – fpiette Nov 20 '20 at 08:49
  • you save my day, thanks a lot. – Ole Nov 20 '20 at 09:26

1 Answers1

0

this code works for me

procedure TForm1.Button1Click(Sender: TObject);
var
  MPdata: TMultipartFormData;
  localfile:String;
  localpath:string;
  LHTTP: TNetHTTPClient;
begin

  localfile := 'myfile.csv';
  localpath := 'C:upload';

  SetCurrentDir(localpath);

  LHTTP := TNetHTTPClient.Create(nil);
  try
    MPdata := TMultipartFormData.Create();
    MPdata.AddFile('file',localfile);
    Memo1.Lines.Text := LHTTP.Post('https://myserver/upload', MPdata).ContentAsString;
  finally
    MPdata.Free;
    LHTTP.Free;
  end;
end;
Ole
  • 1
  • 2
  • You are not supposed post an answer to a question flagged as duplicate. Instead, you should add your answer to the other question provided your answer is different. – fpiette Nov 20 '20 at 10:12