Difference Between curl -d and -F Flags

When using curl to send POST requests, the choice between the -d (--data) and -F (--form) flags determines how your data is formatted and transmitted. This article provides a clear comparison of these two flags, explaining their default Content-Type headers, syntax differences, and specific use cases for web development and API testing.

The -d Flag (application/x-www-form-urlencoded)

The -d or --data flag is used to send standard, URL-encoded data to a server. This is the default method for submitting simple HTML text forms.

Example:

curl -d "username=john_doe&status=active" https://api.example.com/users

The -F Flag (multipart/form-data)

The -F or --form flag is used to simulate a form submission where binary files, large data sets, or mixed data types are sent.

Example (Uploading a file):

To upload a file, prefix the file path with the @ symbol:

curl -F "username=john_doe" -F "profile_pic=@/path/to/avatar.png" https://api.example.com/upload

Key Differences Summary

Feature -d (--data) -F (--form)
Default Content-Type application/x-www-form-urlencoded multipart/form-data
Primary Purpose Sending simple text-based data and API payloads Uploading files and mixed/binary data
File Handling Reads file content as a single text string Uploads file as a binary stream with metadata
Syntax for multiple inputs Combined in a single string: key1=val1&key2=val2 Repeated flags: -F key1=val1 -F key2=val2

By default, both flags automatically instruct curl to make an HTTP POST request. Choose -d for standard API requests and login forms, and choose -F whenever file uploads are required.