简体   繁体   中英

PUT command with Indy 8.0.26 in Delphi 6

I can send a POST request with TIdHTTP in Indy 8, but I don't know how to send a PUT request.

var
  Params:TStrings;
  Resp: TMemoryStream;//TMemoryStream;
  URI, sResp: AnsiString;
begin
   try
      Params := TStringList.Create;
      Params.Add(EdPost.Text);
      //Params.Add('Method=PUT');   ???
      Resp := TMemoryStream.Create;
      URI := EdUri.Text + Edit6.Text;   //
      Http1.Request.ContentType := 'application/json';
      Http1.Request.ExtraHeaders.Values['Authorization'] := 'Bearer ' + sTocken;
      //Http1.Request.ExtraHeaders.Values['Method'] :='PUT';    ???
      Http1.Post(URI, Params, Resp);

TIdHTTP in Indy 8 simply does not support PUT requests. It only supports HEAD , GET , and POST requests. And unfortunately, you can't force a PUT request manually (without altering Indy's source code) as the TIdHTTP.DoRequest() method takes a TIdHTTPMethod enum that defines only those 3 HTTP requests.

Indy 9 added support for additional HTTP requests: OPTIONS , TRACE , PUT , DELETE , and CONNECT .

Indy 10 added support for user-specified HTTP methods, so TIdHTTP is no longer limited to a hard-coded subset of requests.

The current version of Indy (at the time of this writing) is 10.6.2.5457, and still supports Delphi 6 (1) .

I STRONGLY urge you to upgrade away from Indy 8, as it is EXTREMELY old and no longer supported by Indy's developers. Please don't use it. If you do not want to upgrade all the way to Indy 10, then at least upgrade to Indy 9 (though it is no longer supported either, except for bug fixes).

That being said, there is one possible workaround for Indy 8: IF the HTTP server you are sending your data to supports "verb tunneling", then you can use TIdHTTP.Post() to send a POST request while instructing the server to treat the request as if it were PUT instead, eg:

// Sadly, there is no *standard* HTTP header for verb tunneling.
// Different vendors use different headers:
//
// Most vendors use 'X-HTTP-Method-Override'
// Microsoft uses 'X-HTTP-Method'
// IBM uses 'X-METHOD-OVERRIDE'
//
// Send whichever one is appropriate for your particular server, or
// just send them all and let the server work it out...

Http1.Request.ExtraHeaders.Values['X-HTTP-Method-Override'] := 'PUT';
Http1.Request.ExtraHeaders.Values['X-HTTP-Method'] := 'PUT';
Http1.Request.ExtraHeaders.Values['X-METHOD-OVERRIDE'] := 'PUT';
Http1.Post(URI, ...);

(1) : support for pre-Unicode versions of Delphi, which includes Delphi 6, will be dropped in a future Indy 11 maintenance release. Indy 10 will be the last version to support old compilers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM