Jump to content
George Bairaktaris

Wordpress upload media

Recommended Posts

Is there anyone that has uploaded media to wordpress?

 

I am trying to do it with TSslHttpRest but haven't succeeded.

 

Here is the code i use

 

procedure TForm1.UploadImageToWordPress(const URL, Username, Password, FilePath: string);
var
  HttpRest: TSslHttpRest;
  ResponseCode: Integer;
  ResponseText: string;
  aFileStream:TFileStream;
begin
  HttpRest := TSslHttpRest.Create(nil);
  try
    HttpRest.OnHttpRestProg := onSslHttpRestProg ;
    HttpRest.ServerAuth := httpAuthBasic;
    HttpRest.Username := Username;
    HttpRest.Password := Password;
    HttpRest.SslCliSecurity := sslCliSecTls13Only;
    HttpRest.ExtraHeaders.Add('Content-Disposition: "attachment; filename=example.jpg"');
    HttpRest.ExtraHeaders.Add('Content-Type: "image/jpeg"');
    HttpRest.ExtraHeaders.Add('Accept: "*/*"');
    HttpRest.ExtraHeaders.Add('Accept-Encoding: "gzip, deflate, br"');
    HttpRest.ExtraHeaders.Add('Connection: "keep-alive"');
    HttpRest.RestParams.AddItemFile('example.jpg', FilePath, 0);
    HttpRest.HttpUploadStrat := HttpUploadNone;
    ResponseCode := HttpRest.RestRequest(httpPOST, URL, False, '');

    ResponseText := HttpRest.ResponseRaw;
    ShowMessage('Response Code: ' + IntToStr(ResponseCode));
    ShowMessage('Response Text: ' + ResponseText);
  finally
    HttpRest.Free;
  end;
end;

 

No matter what i do i get a 500 error.

I have successfully uploaded with postman or curl.

 

Thank you all

 

 

 

 

Share this post


Link to post

You have not explained how you are attempting to upload the file, what parameters the server is expecting, nor the URL, nor the result of logging from the progress event which would make some of your errors obvious. 

 

The OverbyteIcsSnippets sample has two upload examples, doHttpSimpleUploadClick builds Json command line parameters, HttpUploadFile = file, and HttpUploadStrat := HttpUploadSimple; to POST a binary file, while doHttpFormUploadClick builds a form with a file as a parameter.

 

Your code is similar to the second snippet, but is missing several lines such as RestParams.PContent := PContFormData and has all those ExtraHeaders that duplicate the headers addiws by the component, and which might be the reason for the 500 error.  

 

Angus

 

Share this post


Link to post
8 hours ago, Angus Robertson said:

You have not explained how you are attempting to upload the file, what parameters the server is expecting, nor the URL, nor the result of logging from the progress event which would make some of your errors obvious. 

 

The OverbyteIcsSnippets sample has two upload examples, doHttpSimpleUploadClick builds Json command line parameters, HttpUploadFile = file, and HttpUploadStrat := HttpUploadSimple; to POST a binary file, while doHttpFormUploadClick builds a form with a file as a parameter.

 

Your code is similar to the second snippet, but is missing several lines such as RestParams.PContent := PContFormData and has all those ExtraHeaders that duplicate the headers addiws by the component, and which might be the reason for the 500 error.  

 

Angus

 

That is correct. I missed theese information.

Even i don't have all the answers.

I am using delphi xe2. Unfortunately this is the version my associate uses. If it was my choice i would use restclient with newer version of delphi.

 

I have tried many different approaches.

 

I tried ics demos with all the different parameters with no luck.

 

Media – REST API Handbook | Developer.WordPress.org

This is the documentation they have.

 

All the tests are made based on curl execution. 

curl --request POST \ --url https://b2b.star-toys.com/wp-json/wp/v2/media \ --header "Authorization: Basic ZXJ...ZFFO" \ --header "Content-Disposition: attachment; filename=test.jpg" \ --header "Content-Type: image/jpeg" \ --data-binary "@C:\w\test.jpg"

 

The same logic works with postman.

 

Also postman provides the solution in different languages.

I can understand that they send the image as binaries.

Maybe i am wrong. I don't know.

 

These are the logs and the answer 

 

POST/PUT Parameter Size 77
POST https://b2b.star-toys.com/wp-json/wp/v2/media
example.jpg=C%3A%5Cw%5C3Syn%5Cdocumentation%5Cassets%5Cimages%5C590x300%2Ejpg

https://b2b.star-toys.com/wp-json/wp/v2/media, Getting headers
Connected OK to: b2b.star-toys.com (195.201.214.237)
b2b.star-toys.com SSL Connected OK with TLSv1.3, cipher TLS_AES_256_GCM_SHA384, encryption AESGCM(256), message auth AEAD, group x25519
Request completed: 500 Internal Server Error
Response Code: 500
Response Text: {"code":"rest_upload_sideload_error","message":"\u0394\u03c5\u03c3\u03c4\u03c5\u03c7\u03ce\u03c2, \u03b4\u03b5\u03bd \u03b5\u03c0\u03b9\u03c4\u03c1\u03ad\u03c0\u03b5\u03c4\u03b1\u03b9 \u03bd\u03b1 \u03b1\u03bd\u03b5\u03b2\u03ac\u03c3\u03b5\u03c4\u03b5 \u03b1\u03c5\u03c4\u03cc\u03bd \u03c4\u03bf\u03bd \u03c4\u03cd\u03c0\u03bf \u03b1\u03c1\u03c7\u03b5\u03af\u03bf\u03c5.","data":{"status":500}}

 

 

The message is in Greek says that i can not upload this file format. As can you understand this is not true because the same upload without any problem using postman or curl.

 

So i am out of ideas.

 

Share this post


Link to post

The fact you can use Curl or Postman is not relevant to whether ICS works, it will with the correct parameters. 

 

I've looked at the Wordpress document, and tried some of the examples against a WordPress site I manage, and just get 404 errors, but it is WP 5.2.21 and I'm not the admin, so maybe the APIs are different or I'm not allowed to use them.   The documentation example for 'create a media item'  is ' POST /wp/v2/media' so absolutely useless. 

 

I don't know Curl either so no idea what  --data-binary does. 

 

If you can post the actual http headers, body and response made by Curl or Postman, that should explain what is actually happening.

 

Angus

 

Share this post


Link to post

i am sending you the code worked with Indy 

 

procedure TForm1.UploadImageToWordPress(const URL, Username, Password, FilePath: string);
var
  Params: TIdMultipartFormDataStream;
  aResp:String;
begin
  Params := TIdMultipartFormDataStream.Create;
  Params.AddFormField('content-type','image/jpeg');
  Params.AddFile('file', FilePath, '');
  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
    aResp:=idhttp2.Post(URL, Params);
    LogWin.Lines.Add(aResp);
    LogWin.Lines.Add(IntToStr(idhttp2.ResponseCode));
    LogWin.Lines.Add(idhttp2.ResponseText);
  except
    LogWin.Lines.Add(idhttp2.ResponseText);
  end;
end;
 

 

 

Share this post


Link to post

Extracts of code assume I'm familiar with those applications, I am not. 

 

I asked for the request headers and body content sent, so I know what these applications actually send to the server. 

 

Angus

 

Share this post


Link to post
1 hour ago, George Bairaktaris said:

i am sending you the code worked with

That code and the ICS code you wrote do not send the same parameters!

ContentType is not the same.

ContentDisposition is not the same.

Accept is not the same.

AcceptEncoding is not the same.

With ICS, you restrict SSL cypher to  sslCliSecTls13Only , no restriction specified in the other side.

Data posted must be in form-data format.

And maybe other differences for unspecified properties.

 

François Piette

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×