Search code examples
delphi-xe5http-patch

How do I make a HTTP Patch request in Delphi


This question is in regards to an application being build in Delphi XE5.

I am working with a third party to provide an application that allows users to update information, JSON formatted, through a HTTP post to the third party's API. If I separate the user information into single objects I can use the POST method, but due to the amount if individual events the process is slow. It's much faster if I perform a batch post of a file containing multiple objects but api requires that I use the PATCH verb when uploading a file vs single object. I can do so using curl, but I want to avoid having to install curl on every users system to do so.

Is there any way to utilize the PATCH verb in Delphi? It does not seem that Indy has PATCH supported.


Solution

  • If you are using an up-to-date version of Indy 10, TIdHTTP has 2 overloaded Patch() methods, and a Response.AcceptPatch property, that were added 5 months ago (for use in Embarcadero's REST client):

    procedure Patch(AURL: string; ASource, AResponseContent: TStream);
    function Patch(AURL: string; ASource: TStream): string;
    

    property AcceptPatch: string;
    

    If you are using an older version of Indy, you can call the TIdHTTP.DoRequest() method to send requests using custom verbs. It is declared as protected, so you will have to use an accessor/descendant class to reach it, eg:

    type
      TIdHTTPAccess = class(TIdHTTP)
      end;
    
    TIdHTTPAccess(IdHTTP1).DoRequest('PATCH', URL, SourceData, nil, []);