How to use cURL to send data in JSON format?

To use cURL to send data in JSON format, follow these steps:

  1. Open a terminal or a command prompt.

  2. Write the cURL command followed by the URL you want to send the JSON data to. For example:

    curl -X POST https://example.com/api/endpoint
  3. Add the -H flag followed by 'Content-Type: application/json' to set the request header to JSON format. For example:

    curl -X POST -H 'Content-Type: application/json' https://example.com/api/endpoint
  4. Use the -d flag followed by the JSON data you want to send enclosed in single quotes. For example:

    curl -X POST -H 'Content-Type: application/json' -d '{"key1": "value1", "key2": "value2"}' https://example.com/api/endpoint

    If you have the JSON data in a file, use the @ symbol followed by the path to the file. For example:

    curl -X POST -H 'Content-Type: application/json' -d @data.json https://example.com/api/endpoint

    Note: Make sure the JSON data is properly formatted.

  5. Hit Enter to execute the cURL command and send the JSON data.

This will send the JSON data to the specified URL using cURL.