Detailed explanation of the use of CURL

Initiate GET request

  1. The command is followed directly by the address

    curl 'https://www.baidu.com'
    
  2. Splicing query parameters in request

    curl -G -d 'key=value' 'https://www.baidu.com'
    

    This is equivalent to accessing in the browser address bar https://www.baidu.com?key=value

    If you need to url encode value, it can be written as follows:

    curl -G --data-urlencode 'key=value' 'https://www.baidu.com'
    

    Note that if there is no - G parameter, a POST request will be initiated, and the parameter specified by - d will be submitted in the form of form

  3. Save the response information to a specified file

    curl -o response.txt 'https://www.baidu.com'
    
  4. Add header in request

    curl 'https://www.baidu.com/' \
    -H 'Host: www.baidu.com' \
    -H 'Accept-Language: zh-cn' \
    -H 'Referer: https://www.baidu.com/'
    
    

Initiate POST request

  1. Initiate the simplest POST request

    curl -X POST 'https://www.baidu.com'
    
  2. Submit data as a form

    curl -d 'username=123&password=123' -X POST 'https://www.baidu.com'
    

    -d is the request body

    By adding the - d parameter, the header content type will be set to application/x-www-form-urlencoded by default

    Even after - d is set, the - X POST parameter can be omitted, and the request mode will use the POST mode by default

    curl -d 'username=123&password=123' 'https://www.baidu.com'
    
  3. The form data is disassembled to make it easier to read

    curl 'https://www.baidu.com' \
    -d 'username=123' \
    -d 'password=123' 
    

    When there are multiple parameters, each parameter can be specified with a separate - d

  4. Submit data in JSON format

    curl 'https://www.baidu.com' \
    -H 'Content-Type: application/json' \
    -d '{"username": "123", "password": "123"}'
    

    -H: First, use - h to specify that the submitted data is in json format

    -d: Is the content to be submitted. Here is a string in json format

  5. Load commit data from file

    curl -d '@data.txt' 'https://www.baidu.com'
    

    -The @ symbol is used in the d parameter to specify a file name and tell curl to load the contents of the file as the requested data.

  6. URL encode the submitted data

    The - d parameter mentioned above will not process the requested data, but sometimes we may submit parameters with special characters, such as web address, space, question mark, etc. at this time, we need to url encode the requested data. curl has provided the function of encoding before submitting. Just replace - d with -- data URLEncode-- Other uses of data URLEncode are exactly the same as - d.

    curl 'https://www.baidu.com' \
    --data-urlencode 'username=123' \
    --data-urlencode 'password=123' 
    
  7. Upload file

    curl -F 'file=@image.png' 'https://www.baidu.com'
    

    -F: Use this parameter to specify the file to be uploaded. At the same time, the parameter will automatically set the content type in the header to multipart / form data

  8. Specify the MIME type while uploading the file

    curl -F 'file=@image.png;type=image/png' 'https://www.baidu.com'
    
  9. Upload a file and modify the file name of the uploaded file

    curl -F 'file=@image.png;filename=otherName.png' 'https://www.baidu.com'
    

User Agent modification

  1. Modify with - A parameter

    Use the - A parameter to modify the user agent directly

    #Simulate Firefox
    curl 'https://www.baidu.com' \
    -A 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0'
    
    #Simulate Chrome
    curl 'https://www.baidu.com' \
    -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
    
    #Simulated Safari
    curl 'https://www.baidu.com' \
    -A 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15'
    
  2. Modified by - H

    curl 'https://www.baidu.com' \
    -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15'
    

Modify Referrer

There are also two methods to modify the Referrer. One is to modify it directly through - e, and the other is to modify it in the header through - H

curl 'https://www.baidu.com' \
-e 'https://www.baidu.com'

curl 'https://www.baidu.com' \
-H 'Referer: https://www.baidu.com'

Response redirection

By default, curl does not respond to redirection, that is, it will not perform redirection when the response code is 3XX. You need to specify the - L parameter to respond.

curl -L 'https://www.baidu.com'

silent mode

curl -s 'https://www.baidu.com'

-s hides error messages and progress bars, but prints response information.

If you want to omit the response information, you can use the following command

curl -s -o /dev/null 'https://www.baidu.com'

-o /dev/null will discard the response body

If you want to print the error of the request in silent mode, you can use the following command

curl -S -s -o /dev/null 'https://www.baidu.com'

When - s and - s are used together, the progress bar will still be hidden, but when the request is wrong, an error message will be printed.

Speed limit

#Limit access speed to 1k
curl --limit-rate 1k 'https://www.baidu.com'

#The access speed is limited to 1 byte
curl --limit-rate 1 'https://www.baidu.com'

Print request Headers

curl -v -s -o /dev/null --stderr - 'https://www.baidu.com' | grep '^>'

-v will print out detailed debugging information, - o /dev/null will discard the response body

--stderr - forwards the standard error output to the standard output so that you can use the grep command for filtering.

Filter out the line beginning with > to request Headers

Print response code only

curl -w '%{response_code}' -s -o /dev/null 'https://www.baidu.com'

Do not validate ssl certificate

curl -k 'https://www.baidu.com'

Print response Headers

  1. Only - i will print both Header information and Body information

    curl -i https://www.baidu.com
    
  2. Print Header information only

    curl -s -o /dev/null -D - https://www.baidu.com
    

    -s: the function is not to display the progress bar

    -o /dev/null: used to discard the output body

    -D -: the function is to transfer the headers information to a file. Here, - represents standard output

    The following command seems to print only the header

    curl -I https://www.baidu.com
    

    However, the actual command is the POST request sent, not GET or POST. It can also be seen from the returned header information.

    The lower return length is 2443 and the upper return length is only 277.

    In addition, if some restful interfaces only support GET or POST, a 404 error will be reported when - I is used

Keywords: Linux curl http

Added by cx3op on Sat, 15 Jan 2022 15:02:02 +0200