Setting the HTTP Host header with cURL is a common task, especially when testing server configurations before making DNS changes. If you struggle to remember the right command or want a simpler way to do it, this guide provides both the command and a lazy solution: a Bash function or TCSH alias.

Introduction to Setting the Host Header with cURL

When testing server configurations before DNS changes, you often need to connect to a server (e.g., server.example.com) while pretending to be requesting a different hostname (e.g., www.example.net). This is where the HTTP Host header comes in.

The cURL command for this looks like:

curl --header 'Host: www.example.net' http://server.example.com

But if you’re like me, you may find this syntax hard to remember. To save time and avoid looking it up repeatedly, you can create a Bash function or TCSH alias.

Lazy Solution 1: Bash Function

For Bash users, the solution is to define a custom function, hcurl, which simplifies the syntax:

Bash Function Code

function hcurl() { 
  curl --header "Host: $1" "${@:2}" 
}

How It Works

  • $1 grabs the first argument (the host for the header).
  • ${@:2} passes all other arguments to curl.

This allows you to run:

hcurl www.example.net http://server.example.com

You can also add additional cURL options:

hcurl www.example.net --silent http://server.example.com

Explanation of ${@:2}

  • $@: All arguments passed to the function.
  • ${@:2}: A Bash slice starting from the second argument.

This ensures hcurl passes everything after the first argument directly to curl.

Lazy Solution 2: TCSH Alias

For TCSH users, create an alias instead:

TCSH Alias Code

alias hcurl 'curl --header "Host: \!^" \!:2-$'

How It Works

  • \!^: The first argument (equivalent to !$:1).
  • \!:2-$: All arguments from the second to the last.

You can now run:

hcurl www.example.net http://server.example.com

Example with Additional Options

hcurl www.example.net --silent http://server.example.com

Why Use This?

  • Saves Time: No need to remember the full cURL syntax every time.
  • Flexible: Easily add extra options to your hcurl command.
  • Reusable: Works across Bash and TCSH, adapting to your preferred shell.

Conclusion

With these solutions, you can simplify setting the HTTP Host header with cURL. Whether you choose a Bash function or TCSH alias, you’ll have a quick and reusable tool to streamline your workflow.

Remember:

hcurl www.example.net http://server.example.com

Never struggle with verbose commands again!