CURL Commands


Flag

Description

-A, --user-agent <string>

Specifies the User-Agent string to send to the server.

-b, --cookie <data>

Sends cookies to the server. Can be a string or a file name.

-c, --cookie-jar <file>

Writes cookies to the specified file after the transfer.

-d, --data <data>

Sends data in a POST request. Can be used with application/x-www-form-urlencoded or application/json (with -H).

-F, --form <name=content>

Submits form data. Useful for file uploads and multipart forms.

-G, --get

Forces curl to use the GET method instead of POST when -d is used.

-H, --header <header>

Adds a custom header to the request. Multiple headers can be specified.

-i, --include

Includes the HTTP headers in the output.

-I, --head

Fetches only the HTTP headers from the server.

-k, --insecure

Allows connections to SSL sites without certificates.

-L, --location

Follows redirects (HTTP 3xx responses).

-m, --max-time <seconds>

Sets the maximum time in seconds for the transfer to complete.

-o, --output <file>

Writes the output to the specified file instead of stdout.

-O, --remote-name

Saves the file with the name from the remote URL.

-p, --proxytunnel

Uses the HTTP CONNECT method to tunnel through a proxy.

-s, --silent

Runs curl in silent mode. Suppresses progress meter and error messages.

-T, --upload-file <file>

Uploads a file to the server using the PUT method.

-u, --user <user:password>

Specifies the user name and password for server authentication.

-v, --verbose

Makes curl more talkative. Useful for debugging.

-x, --proxy <[protocol://]host[:port]>

Uses the specified proxy server.

-X, --request <command>

Specifies a custom request method to use (e.g., GET, POST, PUT, etc.).

-z, --time-cond <time>

Downloads files only if they are newer than the specified time.

-#

Displays a progress bar during transfer.

--data-urlencode <data>

URL-encodes the specified data before sending. Useful for form submissions.

--proxy-user <user:password>

Specifies the user name and password for proxy authentication.

--retry <num>

Specifies the number of times to retry a failed transfer.

--trace <file>

Writes a detailed trace of the communication to the specified file.

--trace-ascii <file>

Writes a detailed trace of the communication in ASCII format to the specified file.

--url <url>

Specifies the URL to fetch. This is usually the last option in a curl command.

 

 
The -X flag in curl is used to specify a custom HTTP method to be used for the request. By default, curl uses the GET method for requests, but with -X, you can override this to use other HTTP methods such as POST, PUT, DELETE, PATCH, etc.




Fetch a Web Page

curl http://example.com

Retrieves the content of the specified URL and prints it to the terminal.

Save Output to a File

curl -o filename.html http://example.com

Downloads the content of the URL and saves it to filename.html.

Download a File

curl -O http://example.com/file.zip

Downloads the file from the URL and saves it with the same name as in the URL (e.g., file.zip).

HTTP Methods

  1. GET Request

curl -X GET http://example.com

Sends an HTTP GET request to the specified URL (GET is the default method, so -X GET is optional).


POST Request

curl -X POST http://example.com -d "key=value"

Sends an HTTP POST request with the data key=value to the URL.


PUT Request

curl -X PUT http://example.com/resource -d '{"key":"value"}' -H "Content-Type: application/json"

Sends an HTTP PUT request with JSON data to the specified URL, including the appropriate Content-Type header.

---------------

DELETE Request

curl -X DELETE http://example.com/resource


--------------

Headers and Authentication

Add Headers

curl -H "Authorization: Bearer <token>" http://example.com


Adds custom headers to the request. Here, an Authorization header is included with a bearer token.


--------------


Basic Authentication

curl -u username:password http://example.com

Uses basic authentication with the provided username and password.

--------------

Advanced Usage

  1. Follow Redirects

curl -L http://example.com


--------------
GET AND POST METHODS

In the context of web development and HTTP methods, GET and POST are used to perform different types of operations on web resources. They are two of the most common HTTP methods defined in the HTTP protocol. Here’s a closer look at each:

In the context of web development and HTTP methods, GET and POST are used to perform different types of operations on web resources. They are two of the most common HTTP methods defined in the HTTP protocol. Here’s a closer look at each:

GET Method

  • Purpose: The GET method is used to retrieve data from a specified resource on a server.
  • Characteristics:
    • Data Location: Data is sent in the URL as query parameters (e.g., https://example.com/resource?param1=value1&param2=value2).
    • Idempotent: Calling a GET request multiple times will not change the resource or its state on the server; it only retrieves data.
    • Safe: It should not have any side effects; it is used for requesting information only.
    • Caching: Responses to GET requests can be cached by browsers and intermediary caches to improve performance.
curl -X GET "https://example.com/api/resource?param1=value1&param2=value2"


This command sends a GET request to the URL and retrieves data based on the provided query parameters.
 
 

POST Method

  • Purpose: The POST method is used to send data to a server to create or update a resource.
  • Characteristics:
    • Data Location: Data is included in the body of the request rather than the URL.
    • Non-idempotent: Calling a POST request multiple times may create or modify resources multiple times, which can lead to different results each time.
    • Not Safe: It can have side effects such as creating a new entry in a database or updating existing data.
    • No Caching: Responses to POST requests are typically not cached.
curl -X POST "https://example.com/api/resource" -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}'

Summary

  • GET:

    • Use: Retrieve data.
    • Data Location: URL query parameters.
    • Side Effects: None (idempotent).
    • Caching: Often cached.
  • POST:

    • Use: Submit data to create or update resources.
    • Data Location: Request body.
    • Side Effects: May create or modify resources (non-idempotent).
    • Caching: Typically not cached.

Understanding these methods helps in designing effective web APIs and in debugging issues related to data retrieval and submission.






Comments

Popular posts from this blog

AWK : Commands

$$$$ Symbols in shell scripting