My task is very simple - nevertheless I have been sitting already for hours and have no idea why it doesn't work.
In linux bash script I want to get the result of a webservice call with curl. I'm not interested in the content, only the status code:
#!/bin/bash
set -euo pipefail # put bash in "strict mode"
echo "Before"
response2=$(curl -o /dev/null -s -w '%{http_code}' -u username:password-X POST https://xxxxx.yyyyy.at:8081/MyPath/service/rest/crypto/encrypt -H 'Content-Type: application/json' -d '{"deviceId": "ABCD","payload": "ABCD"}')
echo "after"
It works when there is a valid request
Before...
200
Also, when the path of the service is wrong, it gives http error code
Before...
500
But when the host is wrong (not existent hostname) I get
Before...
and the script terminates (although the call is from a looping menue).
Why is this the case?
The manual call of curl with same parameters gives
000
as output, so why this output is not displayed in my script?
A reproducable example is (server name not existing):
#!/bin/bash
set -euo pipefail
#- Check kms
f_check_kms(){
echo "Before..."
response2=$(curl -o /dev/null -s -w '%{http_code}' -u user:xxxx -X POST https://xxxx.xxx.intra.graz.at:8081/ATM-KeyManagement-REST-Service/service/rest/crypto/encryptUCast -H 'Content-Type: application/json' -d '{"deviceId": "SAG0530000016261", "encryptionSuite": "DLMS_SUITE_0", "securityMode": "AUTHENT_AND_ENCRYPT", "roleId": "001","initialVector": "4D4D4D0000BC614E01234567","payload": "ABCD","usedGuek":"NO","usedGak":"NO"}')
echo "$response2"
}
f_check_kms