Using variable in a CURL command

2

Am trying insert a document into my cloudant as below.

@echo OFF
SPEECH_TEXT="Call me Ishmael. Some years ago-never mind how long precisely-"
curl -X POST -k 'https://<user id>.cloudant.com/testdb' -P 443 -H 'Content-Type:application/json' -d '{"transcript":"'"$SPEECH_TEXT"'"}' | jq-win64.exe

Getting below error:

{
  "error": "bad_request",
  "reason": "invalid UTF-8 JSON"
}

If I replace the variable with plain text in the command, it works fine. Any help much appreciated.

user556526

Posted 2016-02-10T16:12:49.600

Reputation: 21

Answers

1

I normally go the printf route for clean code:

Example:

[user@localhost ~]$ SPEECH_TEXT="something"
[user@localhost ~]$ body=`printf '{"transcript":"%s"}' $SPEECH_TEXT`
[user@localhost ~]$ echo $body
{"transcript":"something"}
[user@localhost ~]$ 

In your case:

curl -X POST -k 'https://cloudant.com/testdb' -P 443 -H 'Content-Type:application/json' -d `printf '{"transcript":"%s"}' $SPEECH_TEXT`

David Betz

Posted 2016-02-10T16:12:49.600

Reputation: 295