Search code examples
delphiindyhttp-options-method

Delphi Indy http OPTIONS


I'd like to use the HTTP Options

OPTIONS /MyURL/DoCmd HTTP/1.1
Origin: http://www.asite.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: access-control-request-method

What is the way to make this with Indy ?

FIdHttp.Options('http://' + FHost + ':' + IntToStr(FPort) + '/MyURL/DoCmd', AResponseContent);

What will happen if the OPTIONS is not implemented by the server ?


Solution

  • What is the way to make this with Indy ?

    TIdHTTP in Indy 10 has 2 overloaded Options() methods:

    procedure Options(AURL: string; AResponseContent: TStream); overload;
    
    function Options(AURL: string
      {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
      ): string; overload;
    

    What TIdHTTP does not have is native properties for the Origin and Access-Control-Request-... headers. However, you can use the TIdHTTP.Request.CustomHeaders property for those, eg:

    FIdHttp.Request.CustomHeaders.Values['Origin'] := 'http://www.asite.com';
    FIdHttp.Request.CustomHeaders.Values['Access-Control-Request-Method'] := ' POST';
    FIdHttp.Request.CustomHeaders.Values['Access-Control-Request-Headers'] := 'access-control-request-method';
    FIdHttp.ProtocolVersion := pv1_1;
    
    Response := FIdHttp.Options('http://www.asite.com/MyURL/DoCmd');
    or
    FIdHttp.Options('http://www.asite.com/MyURL/DoCmd', AResponseContent);
    

    What will happen if the OPTIONS is not implemented by the server ?

    The server will likely return an error code, which TIdHTTP will raise as an EIdHTTPProtocolException exception unless you enable the hoNoProtocolErrorException flag in the TIdHTTP.HTTPOptions property.