Skip to content
shellhacks.dev
APIs

Read the request body from a file

Send a request body from a file with curl -d @file. The option strips newlines from the file, so use --data-binary @file when the exact bytes matter.

1 min read
Summarize in AI

A request body lives in a file and you want curl to send it, and the byte count the server receives is not the one you expected.

Shell
printf 'line one\nline two\n' > body.txt
wc -c < body.txt
curl -s -d @body.txt http://127.0.0.1:8000/
curl -s --data-binary @body.txt http://127.0.0.1:8000/
Text
18
received 16 bytes
received 18 bytes

With @ the argument is a file name, and -d posts its contents as application/x-www-form-urlencoded. The curl manual notes that -d removes carriage returns and newlines from the file, which is why 18 bytes became 16 on the wire. --data-binary sends the file as it is, and it is the right choice for JSON with a trailing newline or any multi-line body.

Neither option sets a content type for you beyond the form default, so add -H 'Content-Type: application/json' for a JSON file. A very large body makes curl send an Expect header and wait for the server to agree, which -H 'Expect:' turns off.

Share

More snippets

APIs

Show only the response headers

Show only the response headers with curl, either by sending a HEAD request or by dumping the headers of a real GET with -D - and dropping the body.

1 min read
APIs

Save a download with its remote name

Save a download under the name in the URL with curl -O. It writes to the current directory, and a URL that ends in a slash has no name to use.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.