Linux Curl Command simulates post/get request

Source: Internet
Author: User
Tags add time http authentication http post

The Curl command is a file transfer tool that works under the command line using URL rules.

Curl supports file uploads and downloads, so it is a comprehensive transfer tool, but traditionally, it is customary to call curl as a download tool.

As a powerful tool, curl supports many protocols including HTTP, HTTPS, FTP, and also supports GET, POST, cookies, authentication, download some files from the specified offset, user agent string, speed limit, file size, progress bar and other features. You can also do Web page processing processes and data retrieval automation.

During web daemon development testing, it is often necessary to send a URL for testing, and using curl to easily simulate a URL command that meets the requirements

Curl Syntax:          curl [Options ...] <url> # Curl (option) (parameter)

Example 1:curl-m--retry 3 //Curl Crawl Web page, get the native extranet IP

Example 2:curl-m--retry 3-x //C URL Crawl Web page through proxy IP, get the native extranet IP

More examples: (Shell curl wget)

Send a GET request using curl: Curl Protocol://ip:port/url?args


Send a POST request using Curl: ( recommended )

Curl-d "Key1=value1&key2=value2&key3=value3" Protocol://ip:port/path

example 1:curl-d ' ' //test post, send what data to return what data, such as ' '

example 2:curl-d "user=admin&passwd=12345678" Https:// //Test post, impersonate the user name and password to send login

This method is the parameter directly inside the header, if you want to specify the output to a file can be manipulated by redirection.

Curl-h "Content-type:application/json"-X post-d ' JSON data ' URL

Example 1:curl-h "Content-type:application/json"-X post-d ' {"Post_data": ""} ' '

Example 2:curl-h "Content-type:application/json"-X post-d ' {"User": "admin", "passwd": "12345678"} ' /login

Curl Application Example

1. File download

The Curl command can be used to perform operations such as downloading, sending various HTTP requests, specifying HTTP headers, and so on.

If the system does not have curl can be installed using Yum Install Curl   , you can also download the installation.

Curl is outputting the download file to stdout, outputting the progress information to stderr, without displaying progress information using the--silent   option.

Curl URL--silent

This command outputs the downloaded file to the terminal, and all downloaded data is written to stdout.

Using the option to -O write the downloaded data to a file, you must use the absolute address of the file:

Curl Http://

Option -o writes the download data to a file of the specified name and uses the --progress Display progress bar: ( common )

Curl Http:// Filename.iso--progress

######################################### 100%

2. Continuation of the breakpoint

Curl can continue downloading from a specific file offset, and it can download some files by specifying a cheap amount:

The Curl url/file-c offset # Offset is an integer in bytes, and if curl automatically infers the correct continuation location using the-C-:curl-c-url

3. Curl Set Reference source page ( common )

The reference page is a string in the HTTP header that indicates from which page the user reached the current page.

If the user clicks on a connection in page A, the user jumps to page b, and the reference page string on page b contains the URL of page A.

Use the --referer options to specify the reference page string:


4. Curl Settings Cookies( common )

Use the--cookie "cokkies"   option to specify a cookie, with multiple cookies separated by semicolons:

Curl "user=root;pass=123456"

To save the cookie as a file, use the--cookie-jar   option:

Curl URL--cookie-jar Cookie_file

5. Curl Set User agent string ( common )

Some Web site access prompts can only be accessed using IE browser, because these sites are set up to check the user agent,

You can use Curl to set the user agent to IE so that you can access it.

Use--user-agent   or -A options:

Curl URL--user-agent "mozilla/5.0" Curl Url-a "mozilla/5.0"

Other HTTP header information can also be sent using curl, using -H "header information" to pass multiple header information, for example:


6. Bandwidth control and download quotas for curl

Use --limit-rate the download speed to limit curl:

Curl URL--limit-rate 50k

command to specify the download speed limit with K (Kbytes) and M (megabytes).

Use--max-filesize   to specify the maximum file size that can be downloaded:

Curl URL--max-filesize bytes

If the file size exceeds the limit, the command returns a non-0 exit code and returns 0 if the command is normal.

7. Curl for Certification

Use the Curl Option-U to complete HTTP or FTP authentication, you can specify a password, or you can enter a password in subsequent operations without specifying a password:

Curl-u user:pwd http://proxy.mimvp.comcurl-u User

8. Curl only prints the response header information ( common )

The HTTP header information can be printed only via-I or -head :

yg-mac:~ homer$ curl-i https://proxy.mimvp.comHTTP/1.1 OKServer:nginxDate:Thu, may 2018 03:51:08 Gmtcontent-type : text/html; Charset=utf-8connection:keep-alivevary:accept-encodingset-cookie:phpsessid=d4kf789gvurb10p7g5pcqd80o6; Path=/expires:thu, 1981 08:52:00 Gmtcache-control:no-store, No-cache, Must-revalidate, post-check=0, pre-check=0 Pragma:no-cachevary:proxys-bj

Curl Parameter Description

-a/--append When uploading a file, attach to the destination file
-a/--user-agent <string> Set up user agent to send to server
-anyauth You can use the "any" authentication method
-b/--cookie <name=string/file> Cookie string or file read location
--basic Using HTTP Basic Authentication
-b/--use-ascii Using ASCII/Text transfer
-c/--cookie-jar <file> The cookie is written to this file after the operation is completed.
-c/--continue-at <offset> Breakpoint Continuation
-d/--data <data> HTTP Post way to transmit data
--data-ascii <data> Post data in ASCII mode
--data-binary <data> Post data in binary mode
--negotiate Using HTTP Authentication
--digest Using digital authentication
--disable-eprt Prohibit the use of eprt or LPRT
--disable-epsv Prohibit the use of EPSV
-d/--dump-header <file> Write header information to the file
--egd-file <file> Set EGD socket path for random data (SSL)
--tcp-nodelay Using the Tcp_nodelay option
-e/--referer Source URL
-e/--cert <cert[:p asswd]> Client certificate file and password (SSL)
--cert-type <type> Certificate file type (Der/pem/eng) (SSL)
--key <key> Private key file name (SSL)
--key-type <type> Private key file Type (Der/pem/eng) (SSL)
--pass <pass> Private key Password (SSL)
--engine <eng> Encryption engine uses (SSL). "--engine List" for list
--cacert <file> CA certificate (SSL)
--capath <directory> CA directory (made using C_rehash) to verify peer against (SSL)
--ciphers <list> SSL password
--compressed Requirement to return is a compressed situation (using deflate or gzip)
--connect-timeout <seconds> Set Maximum request time
--create-dirs Create a directory hierarchy of local directories
--crlf Uploading is converting LF into CRLF.
-f/--fail HTTP error does not appear when connection fails
--ftp-create-dirs If the remote directory does not exist, create a remote directory
--ftp-method [MULTICWD/NOCWD/SINGLECWD] Control the use of CWD
--ftp-pasv Using PASV/EPSV instead of ports
--ftp-skip-pasv-ip When using PASV, ignore the IP address
--ftp-ssl Attempt to use SSL/TLS for FTP data transfer
--ftp-ssl-reqd Requires SSL/TLS for FTP data transfer
-f/--form <name=content> Simulate HTTP form submission data
--form-string <name=string> Simulate HTTP form submission data
-g/--globoff Disable URL sequence and scope using {} and []
-g/--get Send data in a get way
-h/--header <line> Custom header information is passed to the server
--ignore-content-length The length of the ignored HTTP header information
-i/--include Include protocol header information when output
-i/--head Only request header information is displayed
-j/--junk-session-cookies Read file in ignore session cookie
--interface <interface> Use specified network interface/address
--KRB4 <level> KRB4 with the specified security level
-k/--insecure Allow non-use of certificates to SSL sites
-k/--config The specified profile read
-l/--list-only List the file names under the FTP directory
--limit-rate <rate> Set transfer speed
--local-port<num> Forcing the local port number to be used
-m/--max-time <seconds> Set Maximum transfer time
--max-redirs <num> Set the maximum number of read directories
--max-filesize <bytes> Set the maximum amount of files to download
-m/--manual Show Full Manual
-n/--netrc Read the user name and password from the Netrc file
--netrc-optional Overwrite-n with a. netrc or URL
--ntlm Using HTTP NTLM Authentication
-n/--no-buffer Disable buffered output
-o/--output Write the output to the file
-o/--remote-name Writes the output to the file, preserving the file name of the remote files
-p/--proxytunnel Using the HTTP Proxy
--proxy-anyauth Select either Proxy authentication method
--proxy-basic Using Basic authentication on an agent
--proxy-digest Using digital authentication on an agent
--proxy-ntlm Using NTLM authentication on the agent
-p/--ftp-port <address> Use the port address instead of using the PASV
-Q As the first parameter, close the. CURLRC
-q/--quote <cmd> Send command to server before file transfer
-r/--range <range> Retrieving byte ranges from http/1.1 or FTP servers
--range-file Read (SSL) random files
-r/--remote-time Preserve remote file time when generating files locally
--retry <num> Number of retries when there is a problem with the transmission
--retry-delay <seconds> Setting the retry interval when there is a problem with the transport
--retry-max-time <seconds> Set the maximum retry time when there is a problem with the transport
-s/--silent Silent mode. Don't output anything.
-s/--show-error Display Error
--SOCKS4 Using the SOCKS4 proxy for a given host and port
--SOCKS5 Using the SOCKS5 proxy for a given host and port
--stderr <file>
-t/--telnet-option <OPT=val> Telnet option settings
--trace <file> Debug a specified file
--trace-ascii <file> Like--Trace but no hex output
--trace-time When tracking/verbose output, add time stamp
-t/--upload-file <file> Uploading files
--url <URL> Spet URL to work with
-u/--user <user[:p assword]> Set the user and password for the server
-u/--proxy-user <user[:p assword]> Set the proxy user name and password
-w/--write-out [format] What output is complete after
-x/--proxy Using the HTTP proxy on a given port
-x/--request <command> Specify what command
-y/--speed-time The time required to abandon the speed limit is 30 by default
-y/--speed-limit Stop transmission speed limit, speed time

Linux Curl Command simulates post/get request

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: 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.