I'm using git, then posting the commit message and other bits as a JSON payload to a server.
Currently I have:
MSG=`git log -n 1 --format=oneline | grep -o ' .+'`
which sets MSG to something like:
Calendar can't go back past today
then
curl -i -X POST
-H 'Accept: application/text'
-H 'Content-type: application/json'
-d "{'payload': {'message': '$MSG'}}"
'https://example.com'
My real JSON has another couple of fields.
This works fine, but of course when I have a commit message such as the one above with an apostrophe in it, the JSON is invalid.
How can I escape the characters required in bash? I'm not familiar with the language, so am not sure where to start. Replacing '
with '
would do the job at minimum I suspect.
question from:
https://stackoverflow.com/questions/65928757/sanitize-a-string-for-json 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…