我必须使用TIdHTTP组件(Indy 10)发布jpg文件。我需要将文件上传到Drupal 8字段。我可以按照此文档使用Advanced Rest Client来做到这一点:
https://www.drupal.org/docs/8/core/modules/jsonapi-module/file-uploads
https://www.drupal.org/node/3024331#comment-13387295
但是我很想在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;
您不能使用TIdMultiPartFormDataStream
发布文件,因为服务器明确告诉您它不支持multipart/form-data
请求(用TIdHTTP.Post()
调用TIdMultiPartFormDataStream
会覆盖Request.ContentType
)。
您将不得不使用普通香草TStream
来发布文件,例如TFileStream
:
var
response: string;
PostData: TStream;
begin
Result := '';
IdHTTP1.ConnectTimeout := 10000;
IdHTTP1.Request.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.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;
PostData := TFileStream.Create('<path_to>\testimage.jpg', fmOpenRead or fmShareDenyWrite);
try
try
response := IdHTTP1.Post('<my_url_to_the_field_as_instructions>', PostData);
except
on E: Exception do
begin
Memo1.Lines.Add('Error ' + E.message);
end;
end;
finally
PostData.Free;
end;
end;
或者,TIdHTTP
的重载为Post()
,将文件路径作为输入:
var
response: string;
begin
Result := '';
IdHTTP1.ConnectTimeout := 10000;
IdHTTP1.Request.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.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;
try
response := IdHTTP1.Post('<my_url_to_the_field_as_instructions>', '<path_to>\testimage.jpg');
except
on E: Exception do
begin
Memo1.Lines.Add('Error ' + E.message);
end;
end;
end;