Score:0

cURL POST bash script adds single quotes to variables with spaces

us flag

The first 3 variables will always have no spaces. The fourth "slacksitename" has spaces

ip=“x.x.x.x"
record_name=“demo.xyz"
slackuri:”WEBHOOK"
slacksitename:”123 Main St"

When called with cURL

curl -X POST -H 'Content-type: application/json' --data '{"text":"’$slacksitename': '$ip' '$record_name' DDNS updated.”}’ $slackuri

The output is as follows:

curl -X POST -H 'Content-type: application/json' --data '{"text":"123' Main 'St: x.x.x.x demo.xyz DDNS updated."}' https://hooks.slack.com/services/……..
curl: (6) Could not resolve host: Main
curl: (3) unmatched close brace/bracket in URL position 59:
St: x.x.x.x demo.xyz DDNS updated."

Would anyone know why it's adding the single quotes to 123' Main 'St? And how I'd get that to treat the entire variable as one string instead of splitting it (which is what I think it's doing)?

Thanks

UPDATE: Solved^^

Follow Up:

This probably falls along the same lines of the original quetion:

I have a variable that is a JSON output that I want to send via cURL. I'm Assuming the issue is quotations again but the JSON output may change (Number of quotes). Is there any way of sending this raw data with cURL in the message with a new line? Thanks

DUMPING RESULTS:\n{"result":{"id":"ppbkbz2ezmxen11vvpi65chsro1vki5y","zone_id":"unuM0sR1gSrQ37r9fGC1sYKFZOP0DzJM","zone_name":"demo.xyz","name":"1.demo.xyz","type":"A","content":"x.x.x.x","proxiable":true,"proxied":false,"ttl":1,"locked":false,"meta":{"auto_added":false,"managed_by_apps":false,"managed_by_argo_tunnel":false,"source":"primary"},"created_on":"2020-06-14T19:13:57.096688Z","modified_on":"2021-10-16T16:57:49.269274Z"},"success":true,"errors":[],"messages":[]}."
Score:1
gu flag

You're using unquoted variables in bash and look surprised that word splitting happens. This is what's supposed to happen.

You also have non-standard quotes in your question that I'll assume are because of automatic formatting. But note that " is not the same as and ' is not the same as ʼ.

curl -X POST -H 'Content-type: application/json' --data '{"text":"’$slacksitename': '$ip' '$record_name' DDNS updated.”}’ $slackuri

In your command line, you're quoting {"text":" and appending the unquoted content of $slacksitename to it, which means word splitting will take place on it. The end result will be 3 separate arguments: {"text":"123, Main, St. The third argument will be concatenated with : which will itself be concatenated with the content of the unquoted $ip so we're any spaces present in that variable, the same would happen.

This is essentially bash 101, quote your variables unless you know what you're doing.

northport avatar
us flag
Thanks for the reply. The `”` and `ʼ` were by mistake and not actually in the script. I'm a bit lost though on how to solve this. I get what you're saying as far as unquoted variables causing issues. But I'm not sure how a correctly quoted variable would look like. Sorry to be such a noob. Could you show me how to correctly quote the variable or point me in a general direction? I read this stack overflow [thread](https://stackoverflow.com/questions/10067266/when-should-i-wrap-quotes-around-a-shell-variable) and I'm still a little lost. Thanks.
Ginnungagap avatar
gu flag
If you're lost with bash, feel free to refer to its [manual](https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html), especially when you have an idea as to what you're looking for.
Ginnungagap avatar
gu flag
In addition to which, the question you pointed to actually has examples. So what have you tried that didn't work?
northport avatar
us flag
Thanks. The manual helped. Any thoughts on my update^^?
Score:0
es flag

The only way around this that I can find is to save the json to a file and then reference it in the curl call -d "@"

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.