InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Curl

  • Curl handy commands
  • Curl - follow redirects
  • Curl - hide progress bar
  • Curl with download/upload rate limit
 
  • Home
  • > Tutorials
  • > Linux/Unix Command Line
  • > Curl

curl – some handy commands

By admin | Last updated on Mar 15, 2016

Command line utility curl (on Linux, Mac and other operating systems) is pretty handy tool for web developers and can be used to multiple activities. Here are useful ways of using curl for web developers:

View request and response headers using curl

Using -v option request and response headers are printed on stderr. Here is an example:

curl -v https://infoheap.com/

Here is the sample output:

GET / HTTP/1.1
User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8y zlib/1.2.5
Host: infoheap.com
Accept: */*

HTTP/1.1 200 OK
Date: Fri, 02 May 2014 16:00:18 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Vary: Accept-Encoding
X-Pingback: https://infoheap.com/xmlrpc.php
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

Change Host header using curl

Sometimes we need to access a url using server IP or DNS name but change the Host header. This can be useful to hit a server which may be behind load balancer. Here is command to do this:

curl -H 'Host: infoheap.com' http://54.241.26.46/

request for compressed data to be sent using curl

Sometimes we may want to send Accept-Encoding: deflate, gzip so that data on the wire gets compressed. This may be useful when we want the request to be closer to real browser and measure latency.

curl --compressed https://infoheap.com/

Here is the extra request header which gets appended by curl for this request:

Accept-Encoding: deflate, gzip

Provide Basic auth username and password using curl

curl -u myusername:mypassword https://infoheap.com/

If you turn on verbose option -v then you can see that curl sends Authorization: header to the server as shown here:

Authorization: Basic bXl1c2VybmFtZTpteXBhc3N3b3Jk

Fetch page and follow redirection is any (http 301, 302) using curl

By default curl does not follow the redirects. Here is how you can use curl to follow redirection to curl mentioned in Location: header

curl -L https://www.infoheap.com/

Use curl to send page output to a file

Here is how you can save output to a file. It may be useful to downloads, etc.

curl https://wordpress.org/latest.tar.gz -o latest.tar.gz

Suggested posts:

  1. Chrome – copy any request as cURL including headers
  2. Curl – follow redirects
  3. how to use netcat (nc) to test HTTP url redirection
  4. Curl with download/upload rate limit – code snippets
  5. Curl – how to hide progress bar
  6. CORS – cross origin request tutorial and example in PHP
  7. ApacheBench (ab) – how to load test a web site
  8. NFS client and server handy commands
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Curl, Linux, Linux/Unix Command Line, Mac, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2023 InfoHeap.

Powered by WordPress