Alternative to Wget and cURL

If you are working on a Linux device that does not have Wget and cURL, or a limited version of either commands, for instance Busybox, and you need to make some HTTP requests that are more than just a simple GET, there is another option. It is the Netcat networking utility, or often simply ‘nc’.

The example below is an example how you can use nc to make a POST request to a login page, capture the cookie in the response, and then pass along the cookie in a subsequent GET request.

# POST method Login to a site and capture cookie.
LOGIN_BODY_LEN=$( printf "${LOGIN_BODY}" | wc -c )
echo -ne "POST ${LOGIN_POST_PATH} HTTP/1.0\r\nHost: ${HOST}\r\nContent-Type: application/json\r\nContent-Length: ${LOGIN_BODY_LEN}\r\n\r\n${LOGIN_BODY}" | \
  nc -i 2 ${HOST} ${PORT} > /tmp/login_response.txt
COOKIE=`cat /tmp/login_response.txt | grep "Set-Cookie" | sed "s/;.*//;s/.* //"`
rm /tmp/login_response.txt

# Use session cookie in subsequent GET request.
echo -ne "GET ${GET_USER_PATH} HTTP/1.0\r\nHost: ${HOST}\r\nCookie: ${COOKIE}\r\n\r\n" | \
  nc -i 2 ${HOST} ${PORT}

Leave a Reply

Your email address will not be published. Required fields are marked *