Home > Blockchain >  HTTP GET custom header
HTTP GET custom header

Time:02-17

Can't generate a GET request with a custom header.

I have a pretty simple PHP-file on server:

<?php

if (empty($_GET['test']))
    {
        echo('0');
    }
else
    {
        echo('1');
    }
?>

I tried TIdHTTP:

procedure TForm1.Button1Click(Sender: TObject);
begin
  IdHttp1.Request.CustomHeaders.AddValue('test', 'text');
  Memo1.Text:= IdHttp1.Get('https://example.com');
end;

and THTTPClient:

procedure TForm1.Button1Click(Sender: TObject);
begin
  NetHTTPClient1.CustHeaders.Add('test', 'text');
  //NetHTTPClient1.CustomHeaders['test'] := 'text';
  Memo1.Text:= NetHTTPClient1.Get('https://example.com', nil).ContentAsString;
end;

but the document only renders as 0 and /var/log/apache2/error.log contains:

PHP Notice:  Undefined index: test in /var/www/html/index.php on line 3

It seems that CustomHeaders['test'] is empty. Why?

CodePudding user response:

You are mixing up headers with parameters:

  • $_GET contains all parameters when the HTTP method GET was used. This is always the queried address itself: in http://myserver.net/mysite.php?test=hell&other=world you'd have the parameters test and other with their respective values.
  • $_SERVER contains all headers. Headers are what is sent along with requests and responses. Cookies are always headers:
    Connection: keep-alive
    Accept-Ranges: bytes
    Set-Cookie: p=a; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
    

If you want to send and access parameters, then just add them to the address you request:

IdHttp1.Get('https://example.com?test=text');

If you want to access HTTP headers then do that instead of expecting parameters:

if (empty($_SERVER['test']))
  • Related