使用PUT通过TIdHTTP上传文件 [英] Use PUT to upload file with TIdHTTP

查看:151
本文介绍了使用PUT通过TIdHTTP上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在TIdHtttp上使用PUT上传文件.我从雷米·勒博(Remy Lebeau)找到了一个答案,说不使用PUT,而是使用POST.

I'd like to upload a file using PUT on TIdHtttp. I found an answer from Remy Lebeau saying to not use PUT, but use POST instead.

但是,由于我使用的是第三个API,因此我无法执行此操作,并且它指定我需要使用PUT.如果我尝试使用POST,它将返回一条消息,提示不允许使用该方法.

But, in my case I can't do it because I'm using a third API, and it specifies that I need to use PUT. If I try to use POST, it returns me a message saying that the Method is not allowed.

基本上,我正在尝试执行以下操作:

Basically, I'm trying to do something like this:

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.

But when I do it, I get a 500 - Internal server error.

如果我删除了:

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

我得到400 - Bad Request.

我已经尝试直接从浏览器(高级REST客户端-chrome)发出请求,并且该请求有效.因此该API可以正常工作.邮件的标题在起作用时为:

I already tried to do the request directly from browser (Advanced REST client - chrome) and it works. So the API is working. The header of the message when it works is:


PUT /api/2/project/XXXXXXXX/resource/export1po/translation/en/ HTTP/1.1
HOST: www.XXXXXXXXXXXXX.com
authorization: Basic XXXXXXXXXXXXX
content-type: multipart/form-data; boundary=----WebKitFormBoundaryPWT8bUdkQ1ZVLHdL
accept: application/json
accept-encoding: gzip, deflate
accept-language: en-US,en;q=0.8
user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
content-length: 1936   

------WebKitFormBoundaryPWT8bUdkQ1ZVLHdL
Content-Disposition: form-data; name="fileUpload2"; filename="teste.po"
Content-Type: application/octet-stream

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

The code is not here, but yes I configured the authorization information, it's working, I can check this because I can use others call to API.

更新

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

This is the API that I'm trying to use PUT: http://docs.transifex.com/api/translations/#put

我尝试过的方法:

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

1) This one return 500 - Internal Server Error. I tried it creating the DS object with content type application/octet-stream too. But I got the same error.

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.

2) The way that was suggested on answers, but It's not working. I got the error 500 too.

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;

推荐答案

该API被设计破坏.请勿混用multipart/form-dataPUT.您应该这样发送它

That API is broken by design. You should not mix multipart/form-data and PUT. You should send it like this

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

但是,如果它不起作用,则问题可能出在您未设置正确的ContentType,它不包含边界.像这样修复它

But if it does not work, the problem is probably that you don't set a correct ContentType, it does not include boundary. Fix it like this

FHTTP.Request.ContentType:=DS.RequestContentType;

DS是您的TIdMultiPartFormDataStream.

这篇关于使用PUT通过TIdHTTP上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆