Curl usage: Get the header and status code of the website

Source: Internet
Author: User

Curl usage: Get the header and status code of the website

The most common method for curl commands is to use parameter-I to obtain the header information of a domain name or IP address, including the HTTP return status code, server type, text type, and cache time; this method is also often used to determine whether the web Service is normal when monitoring web Services;

To monitor web services, you can use curl to obtain the header of the website and check whether the returned value is 200 OK. This serves as a standard for determining whether the web Service is normal;

Curl-I can be used to obtain the information. If the first line of information is extracted, there will be unnecessary information, how can we obtain it?

[Baby @ localhost ~] $ Curl-I mofansheng.blog.51cto.com
HTTP/1.1 200 OK
Server: Tengine
Date: Thu, 15 Oct 2015 06:10:17 GMT
Content-Type: text/html
Connection: keep-alive
Keep-Alive: timeout = 10
Vary: Accept-Encoding
Set-Cookie: PHPSESSID = 8c0bac037cf2cfd8b87e7dde079eb3bf; path =/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check = 0, pre-check = 0
Pragma: no-cache
Set-Cookie: lastvisit = 0% 091444889417% 09% 2Findex. php % 3F; expires = Fri, 14-Oct-2016 06:10:17 GMT; path =/; domain = .blog.51cto.com
If-Modified-Since: Sat, 10 Oct 2015 16:00:00 GMT
Load-Balancing: web48
Load-Balancing: web48

Use grep to filter the first line and find a lot of unnecessary information.

[Baby @ localhost ~] $ Curl-I mofansheng.blog.51cto.com | grep "OK"

% Total % Received % Xferd Average Speed Time Current

Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 --: -- 0

HTTP/1.1 200 OK

 

Solution:

Man curl: Check whether there are specific parameters that can meet our needs;

-S/-- silent

Silent or quiet mode. Don't show progress meter or error messages.

-S is the silent mode, indicating that no progress table or error message is output;

[Baby @ localhost ~] $ Curl-I-s mofansheng.blog.51cto.com | grep "OK"
HTTP/1.1 200 OK

A command to retrieve the 200 method:

[Root @ bkjia ~] # Curl-s-w "% {http_code}"-o/dev/null www.bkjia.com
200

Other methods: You can direct the error output to the system black hole for filtering.

[Baby @ localhost ~] $ Curl-I mofansheng.blog.51cto.com 2>/dev/null | grep "OK"
HTTP/1.1 200 OK
[Baby @ localhost ~] $ Curl-I mofansheng.blog.51cto.com 2>/dev/null | head-n1
HTTP/1.1 200 OK

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.