I know a similar question was posted, but I can't get it to work on my machine.
I tried the 1st answer from the mentioned question, i.e. response=$(curl --write-out %{http_code} --silent --output /dev/null servername) and when I echo $response I got 000 [Not sure if that is the desired output].
However, when trying to do so with my cURL command, I get no output.
This is my command:
curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
and I use it with
x=$(curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt)
but when I try to echo $x all I get is a newline...
I know the cURL is failing, because when I run the same command, without --silent, I get curl: (7) Couldn't connect to server
This Q is tagged with both sh, bash because I've tried it on both with same results
I found this option which kind of helps (but I still don't know how to assign it to a variable, which should be easier than this...):
--stderr <file>
Redirect all writes to stderr to the specified file instead. If the file name is a plain '-', it is instead written to stdout.
If this option is used several times, the last one will be used.
When I use it like this:
curl -k --silent -S --stderr my_err_file --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
I can see the errors (i.e. curl: (7) Couldn't connect to server) inside that file.
I used --silent to suppress all output, and -S to un-suppress the errors, and the --stderr <file> to redirect them
Related
I ran into a problem when trying to use curl with --netrc-file option in my bash script. When I just put curl -d "username=MYUSR&password=MYPSWD" https://st-machinexxx/api -c cookies.txt
then it works fine. But curl --netrc-file configfile.txt https://st-machinexxx/api -c cookies.txt
causes a HTTP ERROR 401. What can be the reason? I was trying to set athentication method by adding --digest, --negotiate and --ntlm as well as set some headers, but didn't help. I am using curl 7.29.0, configfile.txt contains just three lines:
machine st-machinexxx
login MYUSR
password MYPSWD
I need to run a curl command on a container running in kubernetes cluster via PowerShell. Now, with curl, i need to pass various headers containing long token Strings. However, when i execute the command, the command string gets split in midway and terminal enters into newline mode from which I am not able to exit.
Below is the command.
curl -X GET http://sit-product-abcd-xyzx-adapter:8080/prd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9qpttu2QmA482anQSsS6GN6ltJY9NyjfrW-ElZRrPXunIAf16jOjG0ehEUS8GpUJnc-wStJnF_zYuvnb-4m2GqzXqDHtpEvojg5N0Luq07z2mMpeEXbrGx-0fScjkSAsCJ8E-sFpbBYSXOCHfCslUPjM07kyzaZaa9tgWJGrGA" -H "cache-control: no-cache" -H "content-type: application/json" -H "project-id-type: API"
However, when i enter this command in power-shell after entering into the container, it looks like below.
PS R:\> kubectl exec -it sit-pcapi-order-event-consumer-b6f4c8797-m578d -- sh
/opt/app # curl -X GET http://sit-product-abcd-xyzx-adapter:8080/prdcopy/projects/PROJECT-NOVEMBER2020-eCom-SPORT_STYLE-
3 -H "authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlhSQVFGbGs3V0tNWnFzRm1ZUDcxaE55U3c4TSIsImtpZCI6
IlhSQVFGbGs3V0tNWnFzRm1ZUDcxaE55U3c4TSJ9.eyJhdWQiOiJodHRwczovL2Rldi5hcGkuYWRpZGFzLmNvbS9jb3JwbWFya2V0L3ByZGNvcHkiLCJpc3M
iOiJodHRwOi8vc3RzLXRlc3QuYWRpZGFzLWdyb3VwLmNvbS9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNTYzOTUyOTI5LCJuYmYiOjE1NjM5NTI5Mjk
sImV4cCI6MTU2Mzk1NjUyOSwic3ViIjoic3ZjX3BjYXBpIiwiZGlzdGluZ3Vpc2hlZG5hbWUiOiJDTj1zdmNfcGNhcGksT1U9U2VydmljZUFjY291bnRzLE9
VPVVzZXJzLE9VPUVNQWRtaW5pc3RyYXRpb24sREM9ZW1lYSxEQz1h456bnQsREM9Yml6IiwiYXBwdHlwZSI6IkNvbmZpZGVudGlhbCIsImFwcGlkIjoiNTA4
NzM2YTAtNzM5Mi00ODBkLTlmYWQtYWQ5NzUwMDI3ZjVhIiwiYXV0aG1ldGhvZCI6InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphYzpjbGFzc2VzOlBh
c3N3b3JkUHJvdGVjdGVkVHJhbnNwb3J0IiwiYXV0aF90aW1lIjoiMjAxOS0wNy0yNFQwNzoyMjowOS43NTFaIiwidmVyIjoiMS4wIiwic2NwIjoidXNlcl9p
bXBlcnNvbmF0aW9uIG9wZW5pZCJ9.kBrQO48rK_lkhDeZIQkW_If9lcYn5kERiif1VtlcXAPQ
>
>
>
After this, i need to close the terminal and re-login into the container and kill the sh process manually.
How can i exit this curl command in power-shell? How can i execute long curl commands? I tried powershell_ise, but in that, i am not able to execute kubectl exec command. After executing that command, the terminal remains stuck forever.
Maybe you can copy the token as file into the container/pod and then pass it to curl as file.
More or less like this:
# Copy file
kubectl cp /tmp/token namespace/sit-pcapi-order-event-consumer-b6f4c8797-m578d:/tmp/token
# Pass the token as file instead of literal
kubectl exec -it sit-pcapi-order-event-consumer-b6f4c8797-m578d -- sh /opt/app # curl -X GET http://.../ -H #header_file $URL
Regards
I trying to do execute the following script in bash
#!/bin/bash
source chaves.sh
HEAD='"X-Cachet-Token:'$CACHET_KEY'"'
SEARCH="'{"'"status"'":1,"'"id"'":"'"7"'","'"enabled"'":true}'"
echo $SEARCH
if curl -s --head --request GET http://google.com.br | grep "200 OK" > /dev/null; then
echo 'rodou'
curl -X PUT -H '"Content-Type:application/json;"' -H '"'X-Cachet-Token:$CACHET_KEY'"' -d $SEARCH $CACHET_URL/7
else
echo 'não deu'
curl -X PUT -H '"Content-Type: application/json;"' -H $x -d '{"status":1,"id":"7","enabled":true}' $CACHET_URL/7
fi
But keep receiving a 400 bad request from the server.
When i try to run the same line (echo in the script, Ctrl+c and Ctrl+v) directly in terminal, the command run without problems.
The source file have the directions to path and a variable token i need to use, but as far as i have tested is reading ok.
edit 1 - hidding some sensitive content
edit 2 - posting the exit line (grabed trought Ctrl+c, Ctrl+v)
The command i neet to input in server is:
curl -X PUT -H "Content-Type:application/json;" -H
"X-Cachet-Token:4A7ixgkU4hcCWFReQ15G" -d
'{"status":1,"id":"7","enabled":true}'
http://XXX.XXX.XXX.XXX/api/v1/components/7
And the exit i grabed trought echo comand, give me the exact exit i want, but don't run inside script, only outside.
I'm a bit new to the curl, any help can be apreciate.
Sorry for the bad english and tks in advance.
I am passing username and password to the curl command to receive the output from the api call. Not sure whats wrong, but the curl command works from the command line, but when I use the same curl command within a bash script, it doesn't take the credentials properly. API call fails with an authorization error. Can someone throw some pointers here?
curl --silent -u 'blah-blah:youareawesome$1234' https://example.com/api/check
Here is the script
USERNAME=$1
PASSWORD=$2
curl --silent -u "${USERNAME}:${PASSWORD}" https://example.com/api/check
{"timestamp":1509422967185,"status":401,"error":"Unauthorized","message":"Bad credentials","path":"/api/check"}
Try this
UNAME=$1
PASSWORD=$2
curl --silent -u "${UNAME}:${PASSWORD}" https://example.com/api/check
Blockquote
curl --silent -u 'blah-blah:youareawesome$1234' https://example.com/api/check
This might be a red herring on the quotes but your script won't accept strings after the $ sign.
Might need to wrap your username & password in quotes as you give your script your inputs.
Thank you for all the answers !! For whatever reason, after i modified the script to have the username and password as separate variable and passing to the curl command worked
CREDS="${USERNAME}:${PASSWORD}"
CURL_CMD="curl --silent -u ${CREDS}"
${CURL_CMD} https://example.com/api/check
You can use like this,
example:
curl --location --request POST 'http://119.235.1.63/test' \
--header 'Content-Type: application/json' \
--data-raw '{ "CompanyId":"mafei", "Pword":"mafei123", "SmsMessage":"I am '${USER}'", "PhoneNumber": [ "712955386"] }'
in your code please use single quotes like below
curl --silent -u "'${USERNAME}:${PASSWORD}'" https://example.com/api/check
I set a command line argument as below:
$TOKENARG='Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33'
curl -v -X DELETE -H $(echo $TOKENARG) http://localhost:3001/api/v1/articles/3
And desired result is:
curl -v -X DELETE -H 'Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33' http://localhost:3001/api/v1/articles/3
But when I run the above one it doesn't work as expected and returns the following messages:
curl: (6) Could not resolve host: Bearer
* Rebuilt URL to: 9042f9a3caacc63419be489aefec02a5eae338c33
Could not resolve host: 9042f9a3caacc63419be489aefec02a5eae338c33
How should I pass argument using a variable?
Because you don't quote your command substitution, the string is split up into three words, Authorization:, Bearer and 9042f9a3caacc63419be489aefec02a5eae338c33. To prevent that, you could quote it:
curl -v -X DELETE -H "$(echo $TOKENARG)" http://localhost:3001/api/v1/articles/3
But you don't need echo and the command substitution at all: $(command) has the same output as command, so you can use
curl -v -X DELETE -H "$TOKENARG" http://localhost:3001/api/v1/articles/3
Side note: all uppercase variable names should be used for environment variables only, see for example the POSIX spec (fourth paragraph), so better would be
$tokenarg='Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33'
curl -v -X DELETE -H "$tokenarg" http://localhost:3001/api/v1/articles/3