I was trying to set the user agent dynamic from a bash variable. But it seems that is not that straight forward.
Since the header needs to be enclosed in single quotes if the header value contains spaces, we cannot avoid the single quotes for user agent header, but if we do that the variable wont be evaluated and if we replace it with double quotes, the spaces in ua string will create a lot of mess.
The recommended way is to create all the header, data, cookie variable out side of curl command and use it in a single shot. But does that mean we never ever wont be able to set the user agent variable alone in the curl command?
The command I'm trying to use now is given below,
curl --proxy localhost:8000 -m 1 --retry 0 -i -s -S -b cookie.jar -c cookie.jar -X POST "https://myhost:51512/id?val=${id}" -H 'user-agent: Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405'
to
linux_ua="Mozilla/5.0 (Unknown; Linux) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/v1.0.0 Safari/537.1"
my_user_agent=$linux_ua
curl --proxy localhost:8000 -m 1 --retry 0 -i -s -S -b cookie.jar -c cookie.jar -X POST "https://myhost:51512/id?val=${id}" -H '${user_agent}'
The single quotes are simply shell syntax to prevent any sort of expansion inside the string; they are not part of the header itself. In this case, using double quotes would work just as well.
curl ... -H "user-agent: Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"
Storing the user agent in a variable is trivial:
ua="user-agent: Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"
curl ... -H "$ua"
Here, you must use double quotes to expand the value of $ua; using curl ... -H '$ua' would send the literal 3-character string $ua as a header.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With