cURL

cURL is a command line tool for transfering data. cURL supports the common http, https, ftp, ftps, telnet, pop3, ldap, and many other protocol. I use this for troubleshooting or use in a console where UI is not available. cURL supports sending in any http method (i.e. GET, POST, PUT, DELETE) and also supports sending in the post data. cURL allows setting http request headers.

Getting started

  1. Check if you already have cURL by running this command in the regular OS console.

    
    curl --version
    
    

    The cURL version

  2. If you do not have cURL installed, go to https://curl.se/download.html and download it for your OS. They cover the Windows, MacOS, Linux, and other OSes.

Basics

  1. A basic usage of the curl is a simple GET request.

    
    curl https://localhost:44304/api/example
    
    

    A basic use of cURL's http GET method

  2. A post http method operation can be performed. Use the option -X and then add in the http method, in this case POST. We can add in the post body by using the option -d. On Windows, when sending in post body data, it is necessary to put escape character before the quotation mark like this \".

    The content type also needs to be set for the post body data. For json payload, we use Content-Type: application/json and this needs to be added as a header with the option -H.

    The following example is sending in a json payload in the body.

    
    curl -X POST https://localhost:44304/api/example -d "{ \"key1\": \"value1\"}" -H "Content-Type: application/json"
    
    

    A basic use of cURL http POST method

  3. A PUT http method can be performed by using the option -X PUT. We can pass in the data as we would in a POST http method by using -d option.

    
    curl -X PUT https://localhost:44304/api/example -d "{ \"key1\": \"value1\"}" -H "Content-Type: application/json"
    
    

    A basic use of cURL http PUT method

  4. A DELETE http method can be performed by using the option -X DELETE.

    
    curl -X DELETE https://localhost:44304/api/example/5
    
    

    A basic use of cURL http DELETE method

Other options

  1. We can use the cURL's verbose option, -v, to get more information on the communication between cURL tool and the server.

    
    curl https://localhost:44304/api/example -v
    
    

    cURL showing more information, such as request and response headers and TLS handshake, when using the verbose option

  2. If we need to only look at the response headers instead of the full verbose logging, we can use the -i option.

    
    curl https://localhost:44304/api/example -i
    
    

    cURL showing the response headers

  3. If we are connecting to a server that has self signed certificate or untrusted certificate, we would get an error like this:

    curl showing certificate warning as curl was connecting to a web server with self signed certificate

    If we are sure we are connecting to the correct server but want to ignore this SSL certificate warning about the self signed certificate or untrusted certificate, we can use the -k option. For more details, please go to https://curl.se/docs/sslcerts.html

    
    curl https://localhost:44304/api/example -k
    
    

    using the k option (insecure) to avoid certificate warning during testing

Published on Last updated on