#1 2018-08-29 08:18:36

sevo
Member
Registered: 2015-11-10
Posts: 27

Custom Header in Crossplatform System.Net.HttpClient not sent

Hi Folks,

i want to send binary data (a Picture) with additional HTTP-Header-Data from an Android-App using Firemonkey (FMX) to a method-based service.
But in unit SynCrossPlatformSpecific.pas procedure THttpClientHttpConnectionClass.URI(var Call: TSQLRestURIParams, ...) the HTTP-Headers in parameter Call.InHead are not used/sent.

IMHO the Call.InHead must be filled into a TNetHeader array and passed to the THttpClient.Post/.Put/.. functions.

Here is a working - but naive and memory-leaking function:

procedure TextToNetHeaders(Text : String; var Headers : TNetHeaders);
var
  Pairs : Integer;
  sl : TStringList;
  i, Sep : Integer;
begin
  sl := TStringList.Create;
  try
    sl.Text := Text;
    SetLength(Headers, sl.Count);
    for i := 0 to sl.Count - 1 do begin
      Sep := Pos(':', sl[i]);
      Headers[i] := TNetHeader.Create(Trim(Copy(sl[i], 1, Sep - 1)),
                                      trim(Copy(sl[i], Sep + 1, MAXINT)));
    end;
  finally
    sl.Free;
  end;
end;

Can you please help cleaning up the function/solution and push upstream?

Edit: removed [BUG] from title

Last edited by sevo (2018-08-30 08:18:52)

Offline

#2 2018-08-29 10:17:35

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Custom Header in Crossplatform System.Net.HttpClient not sent

Using of non standard HTTP headers may cause very unexpected errors. Many corporate app firewalls/proxies will remove such headers and server will not got it.

Offline

#3 2018-08-29 11:37:32

sevo
Member
Registered: 2015-11-10
Posts: 27

Re: Custom Header in Crossplatform System.Net.HttpClient not sent

Thx for your reply!

Instead of HTTP-Headers I should use URL-Parameters.

I managed to do so using UrlEncode:

Call.InBody := //binary data from picture
Call.Url := Client.Model.Root + '/' + 'UploadPicture' + UrlEncode(['Param', 123, 'Context', 456]);
Call.Verb := 'POST';
Client.URI(Call);

Why are in UrlEncode only a-zA-Z as parameter names allowed?

According to https://www.ietf.org/rfc/rfc3986.txt the query can consist of *( pchar / "/" / "?" )
Where pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
and unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"

Edit: typo

Last edited by sevo (2018-08-29 11:39:36)

Offline

Board footer

Powered by FluxBB