Added workaround for custom headers not being set correctly.

This commit is contained in:
Andreas Schneider 2015-10-05 13:33:46 +02:00
parent be7615f7dc
commit 2c8721bc20
1 changed files with 26 additions and 1 deletions

View File

@ -83,7 +83,7 @@ implementation
uses uses
strutils, crt, UCRTHelper, fpjson, DOM, XMLRead, XMLWrite, vinfo, strutils, crt, UCRTHelper, fpjson, DOM, XMLRead, XMLWrite, vinfo,
IdCompressorZLib; IdCompressorZLib, IdHeaderList, IdGlobalProtocols;
type type
TContentType = (ctOther, ctJSON, ctXML); TContentType = (ctOther, ctJSON, ctXML);
@ -103,9 +103,30 @@ end;
{ TRestRequest } { TRestRequest }
procedure TRestRequest.Prepare; procedure TRestRequest.Prepare;
var
originalHeaders: TIdHeaderList;
i: Integer;
begin begin
//Parse RawHeaders, since they have precendence and should not be overridden! //Parse RawHeaders, since they have precendence and should not be overridden!
ProcessHeaders; 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; end;
{ TRestHTTP } { TRestHTTP }
@ -390,6 +411,10 @@ begin
FRequest.Prepare; FRequest.Prepare;
FHttp.Request := FRequest; FHttp.Request := FRequest;
FHttp.HTTPOptions := FHttp.HTTPOptions + [hoNoProtocolErrorException]; 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 if SameText('POST', FMethod) and (FFormFields.Count > 0) then
FHttp.Post(AURL, FFormFields, response) FHttp.Post(AURL, FFormFields, response)
else else