When interacting with HTTP servers, different request methods (e.g., GET, POST, PUT, DELETE) communicate specific actions like retrieving, creating, updating, or removing resources.

By default, cURL uses GET if no data is sent, but changing methods enables form submissions, resource uploads, or content deletion. Adjusting the method broadens cURL’s utility for testing various API endpoints.

Ensuring the chosen method aligns with server expectations maintains protocol consistency and compliance with official interface definitions, resulting in correctly interpreted requests.

Steps to specify the HTTP method in cURL:

  1. Open a terminal.
  2. Use --request to change the method from the default GET.
    $ curl --request POST "https://www.example.com/resource"

    Replace POST with PUT, DELETE, or other methods as needed.

  3. Combine --request with --data to send a POST body.
    $ curl --request POST --data "field=value" "https://www.example.com/create"

    Sending data with POST is common for form submissions.

  4. Specify content type and method together as required by the API.
    $ curl --request PUT --header "Content-Type: application/json" --data '{"id":1,"status":"updated"}' "https://www.example.com/update"

    Specifying the method and content type ensures correct server-side parsing.

  5. Verify the server’s response to confirm that the chosen method is understood.
    $ curl --request DELETE "https://www.example.com/delete/1" --verbose

    Check response codes like 200 or 204 indicating successful method execution.

Discuss the article:

Comment anonymously. Login not required.