Search code examples
delphidelphi-xe2puthttp-status-code-405idhttp

IdHTTP.Put Error: HTTP/1.1405 Method Not Allowed


In Delphi XE2, I am trying to upload the lines of a memo to a file on my webspace with IdHTTP.Put:

procedure TForm1.btnUploadClick(Sender: TObject);
var
  StringToUpload: TStringStream;
begin
  StringToUpload := TStringStream.Create('');
  try
    StringToUpload.WriteString(memo.Lines.Text);
    // Error: HTTP/1.1 405 Method Not Allowed.
    IdHTTP1.Put(edtOnlineFile.Text, StringToUpload); 
  finally
    StringToUpload.Free;
  end;
end;

But I always get this error message:

enter image description here

So what must I do to avoid the error and make the upload?


Solution

  • It means the HTTP server does not support the PUT method on that URL (if at all). There is nothing you can do about that. You will likely have to upload your data another way, usually involving POST instead, or a completely different protocol, like FTP.

    BTW, when using TStringStream like this, don't forget to reset the Position if you use the WriteString() method:

    StringToUpload.WriteString(memo.Lines.Text);
    StringToUpload.Position := 0;
    

    Otherwise, use the constructor instead:

    StringToUpload := TStringStream.Create(memo.Lines.Text);