UNIX cUrl command

cUrl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user interaction. I am sharing some of the instances below I've used so far..

  • Basic Authentication
  • # Use -u attribute to provide credentials as <username>:<password>
    # Use -X or --request attribute to mention HTTP method
    # Use --url (applicable by default) to mention destination URL
    curl -X POST -u johndoe01:pass1234 --url https://www.amitpetkar.com
  • Getting HTTP response code from cUrl
  • username='john.doe'
    password='password'
    authURL='http://www.amitpetkar.com/authSignon'
    
    # Getting response text into variable 'response'. 
    # Due to -w ${http_code} attribute, you'd get the HTTP response code 
    # along with end text line of response.
    
    response=$(curl -X POST -w %{http_code} -u $username:$password $authURL);
    
    # If response doesn't contain SUCCESS (HTTP Code 200) then exit.
    if [[ $response != *"200"* ]]; then 
      echo $(date '+%Y-%m-%d-%H:%M:%S')" " "SIGNON Unsuccessful. Exiting..."
      exit;
    fi
  • Getting/Saving Cookie from response
  • cookiePath='./cookie'
    
    # Use --cookie-jar attribute followed by filename which will 
    # contain response header cookies.
    
    # Use backslash (ensure no white space after that) for multi line commands
    curl -X POST -w %{http_code} \
    -u $username:$password \
    --cookie-jar $cookiePath \
    $authURL;
    
    # Reading cookie file contents into variable
    cookies=cat $cookiePath;
  • Saving response into file
  • response='./responseFile.txt'
    
    # Use -o attribute to set cUrl's response into an output file 
    curl -X POST -w %{http_code} \
    -u $username:$password \
    --cookie-jar $cookiePath \
    $authURL -o $response;
  • Use eval() to run any command set in a variable
  • authCurl="curl -X POST -w %{http_code} -u $username:$password $authURL"
    echo "URL to be processed: "$authCurl
    $(eval ${authCurl})
  • Setting cookies for cUrl request
  • JSESSIONID='1234ABCD'
    # Use --header attribute for setting cookie as Cookie: '<cookieValue1><cookieValue2>'
    curl -X POST -w %{http_code} -u $username:$password \
    --header 'Cookie: $JSESSIONID;' $authURL 
    
    # OR use --cookie attribute followed by '<cookieValue1><cookieValue2>'
    curl -X POST -w %{http_code} -u $username:$password \
    --cookie $JSESSIONID $authURL
  • SOAP Request using cUrl
  • requestBody="<some SOAP request>"
    
    # Writing request body into file. 
    # Use > for creating new file every time
    # Use >> for appending over same file
    echo $requestBody > "./requestFile.xml" 
    
    # Use --header attribute to provide request headers
    # Use -d attribute to provide request body.
    soapCurl="curl --request POST \
    --url '$authUrl' \
    --header 'Cookie: $JSESSIONID;' \
    --header 'Content-type: text/xml; charset=UTF-8' \
    --header 'request-type: SOAP' \
    --header 'SOAPAction: \"\"' \
    --header 'cache-control: no-cache' \
    -d @'./requestFile.xml' \
    -o soapResponse.txt";
    
    $(eval ${soapCurl})
    

Comments