1

我想在 TIdHtttp 上使用 PUT 上传文件。我从 Remy Lebeau 那里找到了一个答案,说不要使用 PUT,而是使用 POST。

但是,在我的情况下,我不能这样做,因为我正在使用第三个 API,它指定我需要使用 PUT。如果我尝试使用 POST,它会返回一条消息,指出不允许使用该方法。

基本上,我正在尝试做这样的事情:

Response := TStringStream.Create;
DS := TIdMultiPartFormDataStream.Create;
DS.AddFile('fileUpload2', 'C:\Users\r.rezino\Desktop\teste.po', 'application/octet-stream');
FHttpComunication.Request.ContentType := 'multipart/form-data';
FHttpComunication.Put(UrlCmd, DS, Response);

但是当我这样做时,我得到一个500 - Internal server error.

如果我删除:

FHttpComunication.Request.ContentType := 'multipart/form-data';

我明白了400 - Bad Request

我已经尝试直接从浏览器(高级 REST 客户端 - chrome)发出请求并且它有效。所以API正在工作。消息的标题是:

PUT /api/2/project/XXXXXXXX/resource/export1po/translation/en/HTTP/1.1
主机:www.XXXXXXXXXXXXX.com
授权:基本XXXXXXXXXXXX
内容类型:多部分/表单数据;边界=----WebKitFormBoundaryPWT8bUdkQ1ZVLHdL
接受:应用程序/json
接受编码:gzip,放气
接受语言:en-US,en;q=0.8
用户代理:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
内容长度:1936   

------WebKitFormBoundaryPWT8bUdkQ1ZVLHdL
内容处置:表单数据;名称=“文件上传2”;文件名=“teste.po”
内容类型:应用程序/八位字节流

代码不在这里,但是是的,我配置了授权信息,它正在工作,我可以检查一下,因为我可以使用其他人调用 API。

更新

这是我正在尝试使用的 API :http PUT: //docs.transifex.com/api/translations/#put

我尝试过的方法:

1)这个返回500 - Internal Server Error。我也尝试使用内容类型创建 DS 对象application/octet-stream。但我得到了同样的错误。

Response := TStringStream.Create;
DS := TIdMultiPartFormDataStream.Create;
DS.AddFile('fileUpload2', 'C:\Users\r.rezino\Desktop\teste.po', 'multipart/form-data');
try
    FHttpComunication.Request.ContentType := 'multipart/form-data';
    FHttpComunication.Put(UrlCmd, DS, Response);
    Result := Response.DataString;
finally
    Response.Free;
    DS.Free;
end;

2)答案中建议的方式,但它不起作用。我也得到了错误500

PutData := TFileStream.Create('C:\Users\r.rezino\Desktop\teste.po', fmOpenRead or fmShareDenyWrite);
Response := TStringStream.Create;
try
    FHttpComunication.Request.ContentType := 'multipart/form-data'; //I tried with and without it 
    FHttpComunication.Put(UrlCmd, PutData, Response);
    Result := Response.DataString;
finally
    Response.Free;
    PutData.Free;
end;
4

2 回答 2

6

该 API 被设计破坏了。你不应该混合multipart/form-dataPUT。你应该这样发送

FPutData:=TFileStream.Create('yourfile.dat', fmOpenRead or fmShareDenyWrite);
FHTTP.Put('http://...', FPutData, FResponseStream);

但如果它不起作用,问题可能是你没有设置正确的ContentType,它不包括边界。像这样修复它

FHTTP.Request.ContentType:=DS.RequestContentType;

DSTIdMultiPartFormDataStream.

于 2016-07-27T12:42:19.227 回答
1

我发现了问题。这是关于我必须设定的界限。所以最终的结果是:

DS := TIdMultiPartFormDataStream.Create;
DS.AddFile('fileUpload2', 'C:\Users\r.rezino\Desktop\teste.po', 'application/octet-stream');
try
    FHttpComunication.Request.ContentType := 'multipart/form-data; boundary=' + DS.Boundary;
    FHttpComunication.Request.AcceptEncoding := '';
    FHttpComunication.Request.Accept := '';
    FHttpComunication.Request.Host := '';
    FHttpComunication.Request.UserAgent := '';

    FHttpComunication.Put(UrlCmd, DS, Response);
finally
    DS.Free;
    Response.Free;
end;
于 2016-07-28T11:46:07.960 回答