Http post 如何使用Indy10 Http发布二进制jpg文件?

Http post 如何使用Indy10 Http发布二进制jpg文件?,http-post,delphi-2010,indy10,Http Post,Delphi 2010,Indy10,我必须使用TIdHTTP组件(indy10)发布一个jpg文件。我需要的是上传一个文件到Drupal8字段。我可以按照以下文档使用高级Rest客户端: 但我想用Delphi 2010和Indy 10来做,但没有成功。我总是收到“415不支持的媒体类型”错误,详细信息如下: 未找到与“内容类型:多部分/表单数据”匹配的路由 这是我使用的代码: var response: string; Params: TIdMultiPartFormDataStream; begin

我必须使用TIdHTTP组件(indy10)发布一个jpg文件。我需要的是上传一个文件到Drupal8字段。我可以按照以下文档使用高级Rest客户端:

但我想用Delphi 2010和Indy 10来做,但没有成功。我总是收到“415不支持的媒体类型”错误,详细信息如下:

未找到与“内容类型:多部分/表单数据”匹配的路由

这是我使用的代码:

var
    response: string;
    Params: TIdMultiPartFormDataStream;
begin
    Result := '';

    IdHTTP1.ConnectTimeout := 10000;
    IdHTTP1.Request.Clear;
    IdHTTP1.Request.CustomHeaders.Clear;
    IdHTTP1.Request.BasicAuthentication := false;
    IdHTTP1.Request.ContentType := 'application/octet-stream';
    IdHTTP1.Request.Accept := 'application/vnd.api+json';
    IdHTTP1.Request.ContentLanguage := 'es';
    IdHTTP1.Request.CustomHeaders.AddValue('api-key', 'my_api_key_here');
    IdHTTP1.Request.ContentDisposition:= 'file; filename="testimage.jpg"';

    IdHTTP1.Request.Charset := 'utf-8';
    IdHTTP1.AllowCookies := True;
    IdHTTP1.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36';
    IdHTTP1.HandleRedirects := True;

    Params := TIdMultiPartFormDataStream.Create;
    try
        try
            Params.AddFile('testimage.jpg','c:\tmp\testimage.jpg','application/octet-stream').ContentTransfer:='binary';
            response:= IdHTTP1.Post('<my_url_to_the_field_as_instructions>', Params);
        except
            on E: Exception do
            begin
                memo1.Lines.add('Error ' + E.message);
            end;
        end;
    finally
        Params.Free;
    end;
var
响应:字符串;
Params:TIdMultiPartFormDataStream;
开始
结果:='';
IdHTTP1.ConnectTimeout:=10000;
IdHTTP1.Request.Clear;
IdHTTP1.Request.CustomHeaders.Clear;
IdHTTP1.Request.BasicAuthentication:=false;
IdHTTP1.Request.ContentType:=“应用程序/八位字节流”;
IdHTTP1.Request.Accept:='application/vnd.api+json';
IdHTTP1.Request.ContentLanguage:=“es”;
IdHTTP1.Request.CustomHeaders.AddValue('api-key','my_api_key_here');
IdHTTP1.Request.ContentDisposition:=“file;filename=“testimage.jpg”;
IdHTTP1.Request.Charset:=“utf-8”;
IdHTTP1.AllowCookies:=真;
IdHTTP1.Request.UserAgent:=“Mozilla/5.0(windowsnt 6.1)AppleWebKit/537.36(KHTML,比如Gecko)Chrome/40.0.2214.115 Safari/537.36”;
IdHTTP1.HandlerRedirects:=True;
Params:=TIdMultiPartFormDataStream.Create;
尝试
尝试
Params.AddFile('testimage.jpg','c:\tmp\testimage.jpg','application/octet stream')。ContentTransfer:='binary';
响应:=IdHTTP1.Post(“”,参数);
除了
关于E:Exception-do
开始
备忘录1.行。添加('Error'+E.message);
终止
终止
最后
自由参数;
终止

您不能使用
TIdMultiPartFormDataStream
发布文件,因为服务器明确告诉您它不支持
multipart/formdata
请求(使用
TIdMultiPartFormDataStream
调用
TIdHTTP.post()
会覆盖
Request.ContentType

您必须使用普通的
TStream
来发布文件,例如
TFileStream

var
响应:字符串;
PostData:TStream;
开始
结果:='';
IdHTTP1.ConnectTimeout:=10000;
IdHTTP1.Request.Clear;
IdHTTP1.Request.BasicAuthentication:=false;
IdHTTP1.Request.ContentType:=“应用程序/八位字节流”;
IdHTTP1.Request.Accept:='application/vnd.api+json';
IdHTTP1.Request.ContentLanguage:=“es”;
IdHTTP1.Request.CustomHeaders.AddValue('api-key','my_api_key_here');
IdHTTP1.Request.ContentDisposition:=“file;filename=“testimage.jpg””;
IdHTTP1.AllowCookies:=真;
IdHTTP1.Request.UserAgent:=“Mozilla/5.0(windowsnt 6.1)AppleWebKit/537.36(KHTML,比如Gecko)Chrome/40.0.2214.115 Safari/537.36”;
IdHTTP1.HandlerRedirects:=True;
PostData:=TFileStream.Create('\testimage.jpg',fmOpenRead或fmShareDenyWrite);
尝试
尝试
响应:=IdHTTP1.Post(“”,PostData);
除了
关于E:Exception-do
开始
备忘录1.行。添加('Error'+E.message);
终止
终止
最后
PostData.Free;
终止
终止
或者,
TIdHTTP
有一个重载
Post()
,该重载以文件路径作为输入:

var
响应:字符串;
开始
结果:='';
IdHTTP1.ConnectTimeout:=10000;
IdHTTP1.Request.Clear;
IdHTTP1.Request.BasicAuthentication:=false;
IdHTTP1.Request.ContentType:=“应用程序/八位字节流”;
IdHTTP1.Request.Accept:='application/vnd.api+json';
IdHTTP1.Request.ContentLanguage:=“es”;
IdHTTP1.Request.CustomHeaders.AddValue('api-key','my_api_key_here');
IdHTTP1.Request.ContentDisposition:=“file;filename=“testimage.jpg””;
IdHTTP1.AllowCookies:=真;
IdHTTP1.Request.UserAgent:=“Mozilla/5.0(windowsnt 6.1)AppleWebKit/537.36(KHTML,比如Gecko)Chrome/40.0.2214.115 Safari/537.36”;
IdHTTP1.HandlerRedirects:=True;
尝试
响应:=IdHTTP1.Post(“”,“\testimage.jpg”);
除了
关于E:Exception-do
开始
备忘录1.行。添加('Error'+E.message);
终止
终止
终止