Linux - curl basic use

1 Overview

  1. I have come across a lot of server-side debugging interface testing, eventually fell on this place
  2. Short answer introduce the use of curl
    1. Try step by step, because I'm not familiar with
  3. Probably command mentioned
    1. curl
    2. curl -v
    3. curl -s
    4. curl -i
    5. curl -X
    6. curl -H
    7. curl -d

2. curl

  1. CommandLine Uniform Resource Locator
  2. Official website
    https://curl.haxx.se/
  3. characteristic
    1. This is not to say
    2. After all, it is in my hands, just a simple command-line tool

3. Command

  1. help

    # 这个命令如果能运行, 说明 机器上确实有 curl
    # 具体内容不少, 建议在对 curl 有一定认知之后, 再去尝试阅读
    > curl --help
  2. Simple mode and cumbersome mode
    1. Simple mode
      1. command

        # curl 访问 url 的时候, 建议带上协议
        # 如果不带的话, 默认是 http
        > curl 'http://www.baidu.com'
      2. result
        1. return

          # 勉强对齐了一下
          # 结果类似一个表格, 前两行是表头, 最后一行, 是结果
            % Total    % Received % Xferd  Average Speed   Time    Time   Time  Current
                                           Dload  Upload   Total   Spent    Left  Speed
          100  2381  100  2381    0     0  50659      0 --:--:-- --:--:-- --:--:-- 50659
          # 后面是 baidu 的html, 就不列举了
        2. Sorry, this statistical information, leave a pit bar
          1. If you do not want this statistics, you can use the following command

            > curl -s 'http://www.baidu.com'
    2. Cumbersome mode
      1. command

        > curl -v 'http://www.baidu.com'
      2. result
        1. return

           % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                           Dload  Upload   Total   Spent    Left  Speed
            0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 61.135.169.121...
          * TCP_NODELAY set
          * Connected to www.baidu.com (61.135.169.121) port 80 (#0)
          
          # 这一部分, 是 请求
          > GET / HTTP/1.1
          > Host: www.baidu.com
          > User-Agent: curl/7.63.0
          > Accept: */*
          >
          
          # 这一部分, 是响应
          < HTTP/1.1 200 OK
          < Accept-Ranges: bytes
          < Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
          < Connection: Keep-Alive
          < Content-Length: 2381
          < Content-Type: text/html
          < Date: Fri, 16 Aug 2019 12:48:41 GMT
          < Etag: "588604c4-94d"
          < Last-Modified: Mon, 23 Jan 2017 13:27:32 GMT
          < Pragma: no-cache
          < Server: bfe/1.0.8.18
          < Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
          <
          { [2381 bytes data]
          100  2381  100  2381    0     0  74406      0 --:--:-- --:--:-- --:--:-- 74406
          
          # 这一行, 是请求页面的内容, 略
          
          # 这一行是啥, 我也不太清楚了
          * Connection #0 to host www.baidu.com left intact
          
        2. If you only care about return
          1. You can use the following command

            > curl -i 'http://www.baidu.com'
  3. Request method
    1. command

      > curl -vs -X GET 'http://www.baidu.com'
    2. result
      1. And complicated patterns are basically the same
      2. The default request method is GET
      3. GET can be replaced by other methods
  4. Request header
    1. command

      > curl -is -X GET -H 'Content-Type: text/html' 'http://www.baidu.com'
  5. Request body
    1. command

      > curl -vs -X GET -H 'Content-Type: text/html' -d '{"key": "value"}' 'http://www.baidu.com'

4. Finally

  1. I do not speak good feeling
    1. In fact, these commands can alluded
      1. Is the basic request to see the request, see Response, given request mode setting request header, request body disposed
    2. Related things, I like to talk about and do not know how to start
      1. http relevant
      2. curl results
        1. That statistic
        2. Such as connection process in verbose mode
    3. In fact, this thing has a lot of places you can use
      1. Have the opportunity, I will try to do some other explanation.
  2. ref
    1. curl Guide

      https://www.jianshu.com/p/fc0eb6c60816
    2. Official online book called everything-curl, written in great detail, but they are in English.

Guess you like

Origin www.cnblogs.com/xy14/p/11366565.html