curl options description - unix1998/technical_notes GitHub Wiki

In the context of using curl, which is a command-line tool for transferring data with URLs, several options are commonly used to specify different aspects of an HTTP request:

  1. -d or --data: This option is used to send data in a POST request. It specifies the payload (data) that will be sent to the server. The data can be provided as a URL-encoded string (key1=value1&key2=value2) or in a file (@filename). For example:

    curl -d 'username=user&password=pass' https://example.com/login
    
  2. -D or --dump-header: This option saves the headers from the HTTP response to a file. It can be used to capture the response headers for further inspection or processing. For example:

    curl -D headers.txt https://example.com
    
  3. -H or --header: This option allows you to specify custom HTTP headers for the request. Headers are used to send additional information to the server, such as authentication tokens or content types. Multiple headers can be added by using the option multiple times. For example:

    curl -H 'Content-Type: application/json' -H 'Authorization: Bearer token' https://api.example.com/data
    
  4. -X or --request: This option specifies the HTTP method to be used in the request. curl defaults to using GET if this option is not specified.

    • Common HTTP methods include GET, POST, PUT, DELETE, PATCH, etc. For example:
      curl -X POST https://example.com/api/create
      

Example Usage

Here's how you might combine these options in a practical example:

curl -X POST \
     -H 'Content-Type: application/json' \
     -H 'Authorization: Bearer token' \
     -d '{"name": "John Doe", "age": 30}' \
     https://api.example.com/user/create

In this example:

  • -X POST specifies that this is a POST request.
  • -H 'Content-Type: application/json' sets the Content-Type header to JSON.
  • -H 'Authorization: Bearer token' sets an authorization token.
  • -d '{"name": "John Doe", "age": 30}' sends JSON data as the payload.

These options allow curl to simulate various HTTP requests and customize headers and payloads according to your needs when interacting with APIs or web services from the command line.