How to use cURL to send form data in a POST request?

To send form data in a POST request using cURL, follow these steps:

  1. Set the Content-Type header to application/x-www-form-urlencoded to indicate that the data being sent is URL-encoded form data.
  2. Combine the form fields and values into a single string in the format field1=value1&field2=value2&.... Make sure to properly URL-encode each field and value.
  3. Use the -d or --data option followed by the form data string to specify the data that should be sent in the request body.
  4. Use the -X POST option to specify the HTTP method as POST.
  5. Specify the URL of the target endpoint after all the options.

Here's an example cURL command to send form data in a POST request:

curl -X POST \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "field1=value1&field2=value2" \ http://example.com/endpoint

Replace field1, field2, value1, value2, and http://example.com/endpoint with your actual form field names, values, and target endpoint URL.

Note: If the form data contains special characters that need to be URL-encoded, such as spaces or ampersands, you can use the --data-urlencode option instead of -d to automatically encode the form data:

curl -X POST \ -H "Content-Type: application/x-www-form-urlencoded" \ --data-urlencode "field1=value 1" \ --data-urlencode "field2=value&2" \ http://example.com/endpoint

This command automatically URL-encodes the values value 1 and value&2 in the form data.