Search code examples
wordpressrestdelphiindy

Uploading images to a WordPress WP REST api in Delphi 10.4 via RESTClient or INDY or any others


Can't upload image to my Wordpress site via REST api in Delphi 10.4. I published a post via the REST api (in the REST Debugger and directly in the program), but without pictures it makes no sense.

Most recent code with RESTClient components:

procedure TForm1.Button1Click(Sender: TObject);
var jpgFoto: TMemoryStream;
begin
  HTTPBasicAuthenticator1.Username := 'myuser';
  HTTPBasicAuthenticator1.Password := 'mypass';

  RESTRequest1.Method := TRESTRequestMethod.rmPOST;
  RESTRequest1.Resource := 'wp/v2/media';
  RESTClient1.BaseURL := 'https://*.ru/wp-json';

  RESTRequest1.Params.AddHeader('Content-Disposition', 'attachment; filename="00s.jpg"');//I tried different options
  RESTRequest1.Params.AddHeader('Content-Type', 'image/jpeg');//tried different types, for example multipart or without this line

  RESTRequest1.Params.AddItem('data-binary', 'D:\Europe\00s.jpg');//does it make sense?
  RESTRequest1.Params[0].Kind := pkGETorPOST;

  jpgFoto := TMemoryStream.Create;
  jpgFoto.LoadFromFile('D:\Europe\00s.jpg');
  RESTRequest1.AddBody(jpgFoto, TRESTContentType.ctIMAGE_JPEG);
  jpgFoto.Position := 0;

  try
    RESTRequest1.Execute;
  except
    memo1.Text := RESTResponse1.Content;
  end;

  jpgFoto.Free;

end;

raised exceptions: rest_upload_invalid_disposition, rest_upload_invalid_disposition or rest_upload_no_data (depends on the settings I used in Header RESTRequest)

I found a solution that helped at least one person: switch to INDY to the idHTTP component. But there are no details. I couldn't login through the Basic Authentication method. I set parameters in IDHTTP.Request.Username and IDHTTP.Request.Password, but received a response from Wordpress - 403 Forbidden. It's not clear format to fill params, in JSON or just parameter = value.

I just started learning REST and am stuck for so many hours. Please do not leave in trouble, help upload images in Wordpress api REST with delphi REST or Indy components or any other working way, I will be sooo grateful


Solution

  • It's work:

    var
       Params: TIdMultipartFormDataStream;
    begin
     Params := TIdMultipartFormDataStream.Create;
     Params.AddFormField('content-type','image/jpeg');
     Params.AddFile('file', 'c:\images\sdsd.jpg', '');
     idhttp2.Request.Accept := 'application/json; charset= UTF-8';
     idhttp2.Request.Authentication := TIdBasicAuthentication.Create;
     idhttp2.Request.Authentication.Username:= 'Username';
     idhttp2.Request.Authentication.Password := 'Password';
     idhttp2.Request.BasicAuthentication := true;
    
     idhttp2.Request.ContentDisposition := 'form-data; filename="anyname.jpg"';//
     idhttp2.Request.ContentType := 'application/json; charset= UTF-8';
    
     try
         idhttp2.Post('htts://mysite/wp-json/wp/v2/media/', Params);
     except
       memo1.Text :=  idhttp2.ResponseText;
     end;
    
    end;