0

I am trying to write a script to allow for easy writing to a firebase database using the REST API. Here is the part of the script causing issues:

#!/bin/bash
echo "patient username:"
read USER

echo "name:"
read NAME

echo "birthday:"
read BDAY

echo "insurance provider:"
read INSPROV

echo "insurance number:"
read INSNUMB

echo "group number:"
read GRPNUMB
form0="'"
form10='{\n  "'
form1=${form0}${form10}
form2='": {\n    "'
form3='name": "'
form4='",\n    "birthday": "'
form5='",\n    "insurance provider": "'
form6='",\n    "insurance number": "'
form7='",\n    "group number": "'
form8='"\n  }\n}' 
form9="' "
link="'https://billify-51384-default-rtdb.firebaseio.com/userinfo.json'"

#adding strings together

send1=${form1}${USER}
send2=${form2}${form3}
send3=${send2}${NAME}
send4=${send3}${form4}
send5=${send4}${BDAY}
send6=${send5}${form5}
send7=${send6}${INSPROV}
send8=${send7}${form6}
send9=${send8}${INSNUMB}
send10=${send9}${form7}
send11=${send10}${GRPNUMB}
send12=${send11}${form8}
send13=${send1}${send12}
send14=${send13}${form9}
URL=${send14}${link}

printf "$URL" >> tempfile.txt
URLFX=$(<tempfile.txt)
echo
echo "PRINTING DATA---------------------------------"
printf "$URLFX" 
echo
curl -X PUT -d $URLFX
rm tempfile.txt 

Sorry for all of the string combining, bash is terrible at making it efficient! I put the JSON data into a text file then make the script read it to fix the newline issues (which it seems to do). When I run the script I get this output:

PRINTING DATA---------------------------------
'{
  "testuser": {
    "name": "Johnny Smith",
    "birthday": "January 3, 1903",
    "insurance provider": "CompanyName",
    "insurance number": "101010",
    "group number": "1010"
  }
}' 'https://billify-51384-default-rtdb.firebaseio.com/userinfo.json'
curl: (3) URL using bad/illegal format or missing URL
curl: (3) unmatched brace in URL position 1:
{
 ^

It seems to be formatting everything correctly, as when I add the printed data to the cURL command manually, it works and the data is added. What is cURL's issue with the script? Thank you!

Keolai
  • 19
  • 1
  • 7
  • You need to double-quote `$URLFX` to prevent word-splitting. Also, that string combining is far from the simplest way to do this in bash (& the say you're using `printf` isn't really safe)... but you shouldn't do it directly in bash anyway. Use a proper JSON creation tool, like `jq` (see [this question](https://stackoverflow.com/questions/66432229/convert-environment-variables-into-a-json-file), for example). – Gordon Davisson Mar 07 '21 at 07:01

2 Answers2

1

If you are already storing the string to the file tempfile.txt, why not just upload the file itself.

In addition, you should also set the appropriate headers.

curl -X "POST" "$URL" --data @tempfile.txt -H "Accept: application/json" -H "Content-Type: application/json" -H "User-Agent: MyScript/0.1"
samthecodingman
  • 23,122
  • 4
  • 30
  • 54
1

The immediate problem is the lack of quoting around $URLFX (which is kind of funny, seeing as you quote it correctly when you print it) and the addition of single quotes around the JSON string, which you probably put in in a misdirected effort to fix the quoting problem.

There is a difference between syntactic quotes around values in code, and literal quotes inside those values. Only the former affect how the value is parsed by the shell. Observe the difference between echo ' (a syntax error) and echo "'" (a string consisting of one single quote, in syntactic double quotes to protect the string from being interpreted as syntax).

With that out of the way, try this refactoring.

#!/bin/bash

read -r -p "patient username: " User
read -r -p "name: " Name
read -r -p "birthday: " Bday
read -r -p "insurance provider: " Insprov
read -r -p "insurance number: " Insnumb
read -r -p "group number: " Grpnumb

curl -X PUT -d "\
{
  \"$User\":
  {
    \"name\": \"$Name\",
    \"birthday\": \"$Bday\",
    \"insurance provider\": \"$Insprov\",
    \"insurance number\": \"$Insnumb\",
    \"group number\": \"$Grpnumb\"
  }
}" 'https://billify-51384-default-rtdb.firebaseio.com/userinfo.json'

It is not customary to break up the JSON you send to a URL over multiple lines like this, but Bash can cope just fine. Inside syntactic double quotes, we need to backslash-escape any literal double quotes to preserve them, which is slightly unattractive.

Notice also Correct Bash and shell script variable capitalization and probably read When to wrap quotes around a shell variable?

tripleee
  • 175,061
  • 34
  • 275
  • 318