diff --git a/URestemplateApp.pas b/URestemplateApp.pas index 0ddf7e7..c54b3be 100644 --- a/URestemplateApp.pas +++ b/URestemplateApp.pas @@ -83,7 +83,7 @@ implementation uses strutils, crt, UCRTHelper, fpjson, DOM, XMLRead, XMLWrite, vinfo, - IdCompressorZLib; + IdCompressorZLib, IdHeaderList, IdGlobalProtocols; type TContentType = (ctOther, ctJSON, ctXML); @@ -103,9 +103,30 @@ end; { TRestRequest } procedure TRestRequest.Prepare; +var + originalHeaders: TIdHeaderList; + i: Integer; begin //Parse RawHeaders, since they have precendence and should not be overridden! ProcessHeaders; + + //SetHeaders clears RawHeaders and therefore discards our custom headers. + //We work around that by remembering what is set. + originalHeaders := TIdHeaderList.Create(QuoteHTTP); + try + originalHeaders.Assign(FRawHeaders); + //Now we call SetHeaders to be able to find out what was custom and what + //was known. + SetHeaders; + + for i := 0 to originalHeaders.Count - 1 do + begin + if FRawHeaders.IndexOfName(originalHeaders.Names[i]) < 0 then + FCustomHeaders.Add(originalHeaders.Strings[i]); + end; + finally + originalHeaders.Free; + end; end; { TRestHTTP } @@ -390,6 +411,10 @@ begin FRequest.Prepare; FHttp.Request := FRequest; FHttp.HTTPOptions := FHttp.HTTPOptions + [hoNoProtocolErrorException]; + + // Workaround for CustomHeaders not being assigned :-/ + FHttp.Request.CustomHeaders.AddStrings(FRequest.CustomHeaders); + if SameText('POST', FMethod) and (FFormFields.Count > 0) then FHttp.Post(AURL, FFormFields, response) else