Curl request - how to escape unrecognized character in Bash - bash

I am trying to update a password using curl, see example code below:
# Define the required variables
keystorePassword="Password123"
userPassword="Password321"
user="username"
# Update the password using curl
response=$(curl -s -u "elastic:${keystorePassword}" \
-XPOST "http://localhost:9200/_xpack/security/user/${user}/_password" \
-d'{"password":"'"${userPassword}"'"}' -H "Content-Type: application/json")
echo "Output: $response"
This works OK
But using the below password:
userPassword="Password!/\5"
Returns the error output: "json_parse_exception", "reason":"Unrecognized character escape '5'", "status":400
Is there a way to escape/handle special characters in the userPassword variable?

\ is the escape character. It escapes the character that follows. Since the character that follows is 5, it attempts to escape 5. So, you need to escape \ in order to use it as a character, so use \\5.

Related

What does the `$'...` do in this curl command line? [duplicate]

This question already has answers here:
How does the leading dollar sign affect single quotes in Bash?
(3 answers)
Closed 2 years ago.
Chrome has given me a curl command that I don't fully recognize. What does the $'{...}' do in the following curl command? I know that the --data-binary is the switch to put that string in the body of the POST, but I'm trying to understand if $'... gets "expanded" or something.
curl 'http://localhost.example.com:8080/graphql' \
-H 'cookie: auth.token_key=zztopTuNQ61YYbHGJpfA00' \
-H 'content-type: application/json' \
--data-binary $'{"query":"query workPlanSearch($searchParameters: WorkPlanSearchParameters\u0021) { workPlanSearch( searchParameters: $searchParameters ) { workPlanId } }","variables":{"searchParameters":{"partNumbers":"000-662-7980-001"}},"operationName":"workPlanSearch"}'
Is it a curl thing or a bash thing?
I believe this is duplicated of How does the leading dollar sign affect single quotes in Bash?
It causes escape sequences to be interpreted. < This is the accepted answer in the link
And yes, I believe this is a bashism, take a look:
cat sh_test
#!/bin/sh
echo $'{"name":"joao\npedro"}'
./sh_test
${"name":"joao
pedro"}
cat bash_test
#!/bin/bash
echo $'{"name":"joao\npedro"}'
$ ./bash_test
{"name":"joao
pedro"}
It is used for disambiguation, in your case it means to pass
'{"query":"query workPlanSearch($searchParameters: WorkPlanSearchParameters\u0021) { workPlanSearch( searchParameters: $searchParameters ) { workPlanId } }","variables":{"searchParameters":{"partNumbers":"000-662-7980-001"}},"operationName":"workPlanSearch"}'
as argument
It can used be used for separating a car from text, for example: ${var}text means the content of var and the string text while $vartext means the content of the variable vartext

"Invalid credentials" while doing a curl POST

I have a curl request in below format
curl -v -H "Content-Type:application/json" -H "x-user-id:xxx" -H "x-api-key:yyy" --data '{"logs":"'"${TEST_OUTPUT}"'","pass":"true | false"}' https://razeedash.one.qqq.cloud.com/api/v1/clusters/zzz/api/test_results
This works fine while I do from my MAC terminal. But the same command throws
13:49:26 {
13:49:26 "status": "error",
13:49:26 "message": "Invalid credentials"
13:49:26 }
I saw this post but not sure how else would I send a json body without curly braces. I know that we can save it as a file.json and use the file as body.But for some reasons that cannot be implemented in my scenario
In general, you should avoid trying to build JSON using string interpolation. Use a tool like jq to handle any necessary quoting.
jq -n --argson o "$TEST_OUTPUT" '{logs: $o, pass: "true | false"}' |
curl -v -H "Content-Type:application/json" \
-H "x-user-id:xxx" \
-H "x-api-key:yyy" \
--data #- \
https://razeedash.one.qqq.cloud.com/api/v1/clusters/zzz/api/test_results
However, if you can manage to correctly generate your JSON as you are now, you can just replace the jq command with echo:
echo '{"logs": ...' | curl ...
The #- argument to --data says to read from standard input.

Escape single quote in string and interpolate that string into another string with unescaped single quotes

Trying to get a curl call to work
Here's the ruby code
msg = "john's message"
#body = %Q|{"body":{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"#{msg}"}]}]}}|.gsub("'"){"\\'"}
puts #body
curl_call = %Q|curl -X POST -H 'Content-Type: application/json' -H 'Authorization: Bearer xxxx' -d '#{#body}' --url 'https://api.atlassian.com/site/xxxx/conversation/xxxx/message'|
puts curl_call
system(curl_call)
This is the error: sh: 1: Syntax error: Unterminated quoted string
Open to suggestions
When you replace ' by unicode encoded (\u0027), then it works:
.gsub("'", "\u0027")

bash variable in curl command

In below example (which I got it from PagerDuty webpage):
machine="hi"
curl -H "Content-type: application/json" -X POST \
-d "{ \"service_key\": \"e93facc04764012d7bfb002500d5d1a6\", \"description\": \"FAILURE for production/HTTP on machine $machine\" }" \
"https://events.pagerduty.com/generic/2010-04-15/create_event.json"
I want to use variables in description like:
"description": "FAILURE for $machine",
However it does not work and it only shows me the "FAILURE for $machine",
I tried "FAILURE for ${machine}", but it does not work too. Do you know how to solve it?
The problem is that use use single quotes. You need to use double quotes and escape and double quote in the string:
curl -H "Content-type: application/json" -X POST \
-d "{
\"service_key\": \"e93facc04764012d7bfb002500d5d1a6\",
...
\"description\": \"FAILURE for production/HTTP on machine $machine\"
}" \
"https://events.pagerduty.com/generic/2010-04-15/create_event.json"
Quite tedious, but it will do the job.

How to pass a variable in a curl command in shell scripting

I have a curl command:
curl -u ${USER_ID}:${PASSWORD} -X GET 'http://blah.gso.woo.com:8080/rest/job-execution/job-details/${job_id}'
The variable job_id has a value in it, say, 1160. When I execute the curl command in shell it gives me the following error:
{"message":"Sorry. An unexpected error occured.", "stacktrace":"Bad Request. The request could not be understood by the server due to malformed syntax."}
If I pass the number '1160' directly in the command, as shown below, the curl command works.
curl -u ${USER_ID}:${PASSWORD} -X GET 'http://blah.gso.woo.com:8080/rest/job-execution/job-details/1160'
I want to be able to pass the value of the variable in the curl command.
When using variables in shell, you can only use doubles quotes, not single quotes : the variables inside single quotes are not expanded.
Learn the difference between ' and " and `. See http://mywiki.wooledge.org/Quotes and http://wiki.bash-hackers.org/syntax/words
I ran into this problem with passing as well, it was solved by using ' " $1 " '
See connection.uri below
curl -X POST -H "Content-Type: application/json" --data '
{"name": "mysql-atlas-sink",
"config": {
"connector.class":"com.mongodb.kafka.connect.MongoSinkConnector",
"tasks.max":"1",
"topics":"mysqlstock.Stocks.StockData",
"connection.uri":"'"$1"'",
"database":"Stocks",
"collection":"StockData",
"key.converter":"io.confluent.connect.avro.AvroConverter",
"key.converter.schema.registry.url":"http://schema-registry:8081",
"value.converter":"io.confluent.connect.avro.AvroConverter",
"value.converter.schema.registry.url":"http://schema-registry:8081",
"transforms": "ExtractField",
"transforms.ExtractField.type":"org.apache.kafka.connect.transforms.ExtractField$Value",
"transforms.ExtractField.field":"after"
}}' http://localhost:8083/connectors -w "\n"
How to pass json to curl with shell variable(s):
myvar=foobar
curl -H "Content-Type: application/json" --data #/dev/stdin<<EOF
{ "xkey": "$myvar" }
EOF
With the switch -d or --data, the POST request is implicit
use variable in a double-quote single-quote "' $variable '"
#!/usr/bin/bash
token=xxxxxx
curl --location --request POST 'http://127.0.0.1:8009/submit/expense/' \
--form 'token="'$token'"' \
--form 'text="'$1'"' \
--form 'amount="'$2'"'
userdetails="$username:$apppassword"
base_url_part='https://api.XXX.org/2.0/repositories'
path="/$teamName/$repoName/downloads/$filename"
base_url="$base_url_part$path"**strong text**
curl -L -u "$userdetails" "$base_url" -o "$downloadfilename"

Resources