#1 2017-07-14 09:45:10

jaclas
Member
Registered: 2014-09-12
Posts: 215

Custom HTTP header in client request

How to add custom HTTP header to client request?

Last edited by jaclas (2017-07-14 09:45:38)

Offline

#2 2017-07-21 08:38:09

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Custom HTTP header in client request

Any suggestion?
Do I have to override this method:

function TSQLHttpClientRequest.InternalRequest(const url, method: RawUTF8; var Header, Data, DataType: RawUTF8): Int64Rec;

?

Offline

#3 2017-07-21 12:11:31

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,669
Website

Re: Custom HTTP header in client request

Why do you need to mess the HTTP headers?

Do you expect this with interface-based services or method-based services?

Offline

#4 2017-07-21 13:12:03

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Custom HTTP header in client request

I work with interface-based service.
I want add some header for debug purpose. In header because I can add in one place to debug in all services at once.

Offline

#5 2017-07-21 13:37:37

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,669
Website

Re: Custom HTTP header in client request

There is no simple way of doing this on the client side... unless you override the low-level HTTP classes...

Offline

#6 2017-07-22 07:09:37

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Custom HTTP header in client request

I did this:

type
  TMySQLHttpClientRequest = class(TSQLHttpClientWinHTTP)
  protected
    function InternalRequest(const url, method: RawUTF8; var Header, Data, DataType: RawUTF8): Int64Rec; override;
  end;


implementation

function TMySQLHttpClientRequest.InternalRequest(const url, method: RawUTF8; var Header, Data, DataType: RawUTF8): Int64Rec;
begin
  Header := 'myheader: somedata';
  Result := inherited InternalRequest(url, method, Header, Data, DataType);
end;

And it works :-)


Are there any hidden side effects here?

Offline

#7 2017-07-22 16:12:27

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,669
Website

Re: Custom HTTP header in client request

If the caller set a header, it won't work.
You should test for any existing header.

Offline

#8 2017-07-25 13:23:29

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Custom HTTP header in client request

You mean should I do something like this:

 if Header <> '' then
 begin
  Header := Header + #13#10 + 'myheader: somedata';
 end;

?

Offline

#9 2017-07-25 16:32:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,669
Website

Re: Custom HTTP header in client request

Or more correctly:

if Header <> '' then
  Header := Header + #13#10;
Header := Header + 'myheader: somedata';

Or even:

Header := AddToCSV('myheader: somedata', Header, #13#10);

Offline

#10 2017-07-25 18:47:17

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Custom HTTP header in client request

Thanks for tip with AddToCSV!

But I have one question.

This code:

if Header <> '' then
  Header := Header + #13#10;
Header := Header + 'myheader: somedata';

is better than:

if Header <> '' then
 begin
  Header := Header + #13#10 + 'myheader: somedata';
 end;

...because? You mean better memory management? Less allocations? Or...?

Offline

#11 2017-07-25 19:39:14

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,669
Website

Re: Custom HTTP header in client request

The 2nd do not append 'myheader: somedata' to Header.

Offline

#12 2017-07-25 20:37:38

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Custom HTTP header in client request

Oops... My total disgrace! Sorry :-)

Offline

Board footer

Powered by FluxBB