Pobieranie tylko nagłówka odpowiedzi z POSTU HTTP za pomocą curl

Można żądać tylko nagłówków używając HTTP HEAD, jako opcja -I w curl(1).

$ curl -I /

Długie ciała odpowiedzi HTML są bólem, aby dostać się w wierszu poleceń, więc chciałbym uzyskać tylko nagłówek jako opinię dla moich żądań POST. Jednak HEAD I POST to dwie różne metody.

Jak sprawić, by curl wyświetlał tylko nagłówki odpowiedzi na żądanie POST?

Author: Jonathan Allard, 2012-04-08

6 answers

-D, --dump-header <file>
       Write the protocol headers to the specified file.

       This  option  is handy to use when you want to store the headers
       that a HTTP site sends to you. Cookies from  the  headers  could
       then  be  read  in  a  second  curl  invocation by using the -b,
       --cookie option! The -c, --cookie-jar option is however a better
       way to store cookies.

I

-S, --show-error
       When used with -s, --silent, it makes curl show an error message if it fails.

I

-L/--location
      (HTTP/HTTPS) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response
      code), this option will make curl redo the request on the new place. If used together with -i/--include or -I/--head, headers from  all  requested
      pages  will  be  shown.  When authentication is used, curl only sends its credentials to the initial host. If a redirect takes curl to a different
      host, it won’t be able to intercept the user+password. See also --location-trusted on how to change this. You can limit the amount of redirects to
      follow by using the --max-redirs option.

      When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the HTTP
      response was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following  request  using  the  same  unmodified
      method.

Ze strony man. więc

curl -sSL -D - www.acooke.org -o /dev/null

Śledzi przekierowania, zrzeka nagłówki na stdout i wysyła dane do /dev / null (TO JEST GET, nie POST, ale możesz zrobić to samo z postem - po prostu dodaj dowolną opcję, której już używasz do wysyłania danych)

Zwróć uwagę na - Po -D, co oznacza, że wyjściowym "plikiem" jest stdout.

 626
Author: andrew cooke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-19 22:42:22

Pozostałe odpowiedzi wymagają pobrania ciała odpowiedzi. Ale jest sposób na wysłanie żądania POST, które pobierze tylko nagłówek:

curl -s -I -X POST http://www.google.com

-I sam wykonuje żądanie HEAD, które może zostać nadpisane przez -X POST, aby wykonać żądanie POST (lub inne) i nadal uzyskać tylko dane nagłówka.

 94
Author: siracusa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-22 17:34:44

Dla ciał o długiej odpowiedzi (i różnych innych podobnych sytuacjach), rozwiązaniem, którego używam, jest zawsze podłączenie do less, więc

curl -i https://api.github.com/users | less

Lub

curl -s -D - https://api.github.com/users | less
Wykonam zadanie.
 38
Author: fiatjaf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-05 13:44:44

Poniższe polecenie wyświetla dodatkowe informacje

curl -X POST http://httpbin.org/post -vvv > /dev/null

Możesz poprosić serwer o wysłanie tylko głowy, zamiast pełnej odpowiedzi

curl -X HEAD -I http://httpbin.org/

Note: Niektóre nieprawidłowo skonfigurowane / zaprogramowane Serwery WWW mogą odpowiedzieć inaczej niż post, ponieważ jest to żądanie głowy, a nie POST. Ale działa przez większość czasu

 35
Author: zainengineer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-30 20:38:59

Znacznie łatwiejsze – to jest to, czego używam, aby uniknąć śledzenia Shortlinku {[3–} - jest następujące:

curl -IL http://bit.ly/in-the-shadows

...który również następuje po linkach .

 14
Author: kaiser,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-10 21:18:03

Podczas gdy inne odpowiedzi nie sprawdziły się u mnie w każdej sytuacji, najlepsze rozwiązanie jakie mogłem znaleźć (praca z POST również), zaczerpnięte z TUTAJ :

curl -vs 'https://some-site.com' 1> /dev/null

 11
Author: Daniel A. R. Werner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-19 15:49:18