Q:

Cannot expand and substitute the contents of a variable in Bash

I am using Gitbash, GNU bash, version 4.3.46(2)-release (x86_64-pc-msys). 
I am running a bash script that looks like this
CODE CHUNK 1
curl -i -X POST \
   -H "Content-Type:application/json" \
   -H "x-customheader:customstuff" \
   -d \
'{
  Gigantic json payload contents in here
}' \
'http://localhost:5000/api/123'

This works fine.  Basically it posts a giant payload to an endpoint and all is well. The problem is when I attempt to substitute the url for a value from a variable, I get a curl error,
curl: (1) Protocol "'http" not supported or disabled in libcurl
CODE CHUNK 2
stuff=\'http://localhost:5000/api/123\'
curl -i -X POST \
   -H "Content-Type:application/json" \
   -H "x-customheader:customstuff" \
   -d \
'{
  Gigantic json payload contents in here
}' \
$stuff

If I echo $stuff immediately after the stuff=\'http://localhost:5000/api/123\', I get 'http://localhost:5000/api/123'.  This is the same value as I had hard-coded in code chunk 1, single ticks and all. There is something hiding behind the scenes in how that url is being evaluated after the variable has been expanded.  I need to get the same behavior as a hard coded url.

A:

Look closely at this error message:

curl: (1) Protocol "'http" not supported or disabled in libcurl

Notice the single-quote in front of http.
The curl command surely knows the http protocol, but not the 'http protocol!
The way you wrote it, the single-quotes are part of the value of stuff:

stuff=\'http://localhost:5000/api/123\'

Remove those, write like this:
stuff='http://localhost:5000/api/123'

If you have variables inside your real string,
and you want them expanded, then use double-quotes instead of single-quotes:
stuff="http://localhost:5000/api/123"

Equally important,
when you use $stuff as a parameter of curl,
you must double-quote it.
If you just write curl $stuff,
then the shell may interpret some characters in $stuff before passing to curl.
To protect from that,
you must write curl "$stuff".
The complete command:
curl -i -X POST \
   -H "Content-Type:application/json" \
   -H "x-customheader:customstuff" \
   -d \
'{
  Gigantic json payload contents in here
}' \
"$stuff"

Finally, make sure that after each \ at the end of lines,
there's nothing after the \ on each line,
the \ must be at the very end.

