【发布时间】:2025-11-30 18:50:01
【问题描述】:
无法在 Delphi 10.4 中通过 REST api 将图片上传到我的 Wordpress 网站。 我通过 REST api 发布了一篇文章(在 REST 调试器中,直接在程序中),但是没有图片就没有意义。
带有 RESTClient 组件的最新代码:
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;
引发的异常:rest_upload_invalid_disposition、rest_upload_invalid_disposition 或 rest_upload_no_data(取决于我在 Header RESTRequest 中使用的设置)
我发现一个solution 至少帮助了一个人:切换到INDY 到idHTTP 组件。但没有细节。我无法通过基本身份验证方法登录。我在 IDHTTP.Request.Username 和 IDHTTP.Request.Password 中设置了参数,但收到了来自 Wordpress 的响应 - 403 Forbidden。填写参数的格式不明确,用 JSON 或只是 parameter = value。
我刚开始学习 REST 并被困了这么多小时。请不要留下麻烦,使用delphi REST或Indy组件或任何其他工作方式帮助在Wordpress api REST中上传图像,我将非常感激
【问题讨论】:
标签: wordpress rest delphi indy