BudiBadu Logo
Samplebadu

cURL by Example: Send Binary Data

Latest

Send binary data directly from the command line (if possible) or via pipe.

Code

# Pipe binary data into cURL
cat image.png | curl -X POST -H "Content-Type: image/png" --data-binary @- https://api.example.com/upload

# Send hex-encoded data (rare but possible)
# (Requires converting hex to binary first, usually via other tools)

Explanation

The @- syntax tells cURL to read binary data from standard input instead of a file. This enables you to pipe output from other commands directly into your HTTP request.

Common use cases for piping binary data:

  • Compressing data on the fly with gzip before uploading
  • Streaming output from tar for backup uploads
  • Processing images with ImageMagick before sending to an API
  • Encrypting data with openssl during transmission
  • Generating dynamic content from scripts or database queries

This approach saves disk space since you don't need to create temporary files. The data flows directly from one command to cURL and then to the server. The --data-binary flag ensures the piped data isn't modified during transmission.

When piping data, you still need to set the correct Content-Type header manually since cURL can't detect the type from a pipe. For example, use -H "Content-Type: application/gzip" when piping compressed data.

Code Breakdown

2
cat image.png | ... streams file content to stdout.
2
--data-binary @- reads from stdin.
All
Enables powerful command-line pipelines.