delphi将文件作为字节数组发送到Rest服务 [英] delphi Send a file as byte array to a Rest service
本文介绍了delphi将文件作为字节数组发送到Rest服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Delphi 10.1 Berlin
I'm using Delphi 10.1 Berlin
我想使用 TRestRequest
将图像数据作为 TBytes
发送到Rest服务,但是我找不到传递 TBytes
的方法到 TRestRequest.AddBody()
方法或任何其他方法.
I want to send image data as TBytes
to a Rest service using TRestRequest
, but I could not find a way to pass a TBytes
to the TRestRequest.AddBody()
method, or any other method.
POST http://myserver:1111//Openxxx/RecxxxLxxxPxxxx HTTP/1.1
Content-Type: text/json
Host: myserver:1111
Content-Length: 28892
Expect: 100-continue
Connection: Keep-Alive
[255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,255,219,0,132,0,9,
...
...
...
130,130,252,168,127,164,63,164,41,109,204,245,62,106,51,135,12,146,63,255,217]
推荐答案
我已经解决了如下问题:
I've solved my problem like below:
function BytesToStr(abytes: tbytes): string;
var
abyte: byte;
begin
for abyte in abytes do
begin
Result := result + IntToStr(abyte) + ',';
end;
Result := '[' + Copy(Result, 1, Length(Result) - 1) + ']';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ABytes: TBytes;
begin
ABytes := TFile.ReadAllBytes('images.jpg');
RESTRequest1.Params.AddItem('body', BytesToStr(ABytes), pkREQUESTBODY, [], ctAPPLICATION_JSON);
RESTRequest1.Execute;
end;
这篇关于delphi将文件作为字节数组发送到Rest服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文