A
A
Atraides2018-08-21 10:47:20
bash
Atraides, 2018-08-21 10:47:20

How to pass parameter to CURL with space or other specials characters?

There is a simple get request to the Bitrix API:

curl -H "Content-Type: application/json" -d '['$taskid',{"TAGS":"'$oldtag',камера_1_off"}]' https://????.bitrix24.ru/rest/66/??????/task.item.update

as the $oldtag parameter, a string is passed in which words are separated by a comma, there can be spaces between words, for example: tag1, another tag, tag two . The task is to add the phrase - ,camera_1_off to the oldtag parameter.
So the problem is that in the bash script, for some reason, the space is not correctly transmitted through the $oldtag variable. Curl takes it as a delimiter. Although if you manually enter the test in the "TAGS" field in the terminal: "tag one, tag two" - then everything works. The question is how to pass text inside a bash script.
Whole script:
#!/bin/bash

taskid="$2"

#Парсит существующие теги
oldtag="$(curl -H "Content-Type: application/json" -d '[17267]' https://?????????.bitrix24.ru/rest/66/????????/task.item.getdata | jq -c '.result.TAGS' |  sed s/\"//g | sed s/\\[//g | sed s/\]//g)"


#oldtag=тег, новый тег, еще тег

#записывает старый тег+новый
curl -H "Content-Type: application/json" -d '['$taskid',{"TAGS":"'$oldtag',камера_1_off"}]' https://????.bitrix24.ru/rest/66/??????/task.item.update

5b7bc19a18eac364552823.png
This is what the error looks like.
And this is how it looks if you pass the text of the tags, without spaces. everything works:
5b7bc2933edd6745694333.png
In principle, in CURL you can escape characters by adding "\" before the character with sed, but maybe there is a simpler solution ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2018-08-21
@Atraides

Take it in quotes.
Here, from a hodgepodge of single and double quotes, you generally give $oldtag to a string without quotes. Bash parses it.
Try like this
"[$taskid,{\"TAGS\":\"$oldtag,камера_1_off\"}]"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question