0

I'm trying to create folder on OneDrive. This code works OK with XE8 + Indy 10. But with Indy 9 and Delphi 7 two times from three or so I have: "EIdHTTPProtocolException with message "http/1.1 404 not found". The folder is created anyway.

Is there a way to avoid the exception with Indy 9? I can't use Indy 10 in Delphi 7 becouse of specification.

procedure OneDriveCreateFolderTest;
const
  URL = 'https://api.onedrive.com/v1.0/drive/root::/children';
var
  IdHttp: TIdHttp;
  IdSSLIOHandlerSocket: TIdSSLIOHandlerSocket; // XE8 => TIdSSLIOHandlerSocketOpenSSL
  Stream: TStringStream;
begin
  IdHttp := TidHTTP.Create(nil);
  try
    IdHttp.HandleRedirects := True;
    IdSSLIOHandlerSocket := TIdSSLIOHandlerSocket.Create(IdHttp);
    IdSSLIOHandlerSocket.SSLOptions.Method := sslvTLSv1;
    IdHttp.IOHandler := IdSSLIOHandlerSocket;
    IdHttp.Request.CustomHeaders.FoldLength := MaxInt;
    IdHttp.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + FAccessToken;
    IdHttp.Request.BasicAuthentication := False;
    IdHttp.Request.ContentType := 'application/json';
    Stream := TStringStream.Create('{ "name": "TestDir", "folder": { } }');
    try
      IdHttp.Post(URL, Stream);
    finally
      Stream.Free;
    end;
  finally
    IdHttp.Free;
  end;
end;
  • @mjn42 Thanks, I know about Indy 10. But is there any chance with Indy 9? – Oleg Adibekov Feb 06 '17 at 12:06
  • @mjn42 No, I will try it, thanks. – Oleg Adibekov Feb 06 '17 at 13:27
  • 404 means the URL being requested does not exist on the server. That should not have anything to do with a particular Indy version. However, you can attach one of Indy's `TIdLog...` components to `TIdHTTP`, or use an external HTTP debugger like Fiddler, to see the actual HTTPS requests being sent. There may be a difference between Indy 9 and 10 requests that is confusing OneDrive. – Remy Lebeau Feb 06 '17 at 18:11
  • @RemyLebeau, thanks. I tried `IdHttp.HandleRedirects := False;` and result is EIdHTTPProtocolException with message 'HTTP/1.1 201 Created'. I think I will ignore EIdHTTPProtocolException by `try .. except`. – Oleg Adibekov Feb 06 '17 at 20:21
  • a 201 response includes a `Location` header. `TIdHTTP` in Indy 9 has a bug where it assumes **any** response with a `Location` header is a redirect and acts accordingly. `TIdHTTP` in Indy 10 fixed that by looking at the response code first and doing redirection logic only for actual redirect codes. – Remy Lebeau Feb 06 '17 at 23:06

0 Answers0