How to escape a JSON variable posted with curl in bash script?

I'm submitting a message to Mandrill in a bash script via their API and the content of the 'message' variable is causing the API call to come back with an error:

An error occured: {"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}

The content of the $message_body variable is:

Trigger: Network traffic high on 'server'
Trigger status: PROBLEM
Trigger severity: Average
Trigger URL:

Item values:

1. Network traffic inbound (server:net.if.in[eth0,bytes]): 3.54 MBytes
2. Network traffic outbound (server:net.if.out[eth0,bytes]): 77.26 KBytes
3. *UNKNOWN* (*UNKNOWN*:*UNKNOWN*): *UNKNOWN*

Original event ID: 84

I'm not sure what part of the string is throwing it off, but it seems that something is causing the JSON to be invalid when submitted to Mandrill's API.

If I change the above message to something simple such as "Testing 123" the message gets submitted successfully.

The code that does the POST is as follows:

#!/bin/bash
...
message_body = `cat message.txt`
msg='{ "async": false, "key": "'$key'", "message": { "from_email": "'$from_email'", "from_name": "'$from_name'", "headers": { "Reply-To": "'$reply_to'" }, "auto_html": false, "return_path_domain": null, "subject": "'$2'", "text": "'$message_body'", "to": [ { "email": "'$1'", "type": "to" } ] } }'
results=$(curl -A 'Mandrill-Curl/1.0' -d "$msg" 'https://mandrillapp.com/api/1.0/messages/send.json' -s 2>&1);
echo "$results"

What can I do to make sure the $message_body variable is prepared and ready to be submitted as valid JSON?


I suspect the problem is the lack of quoting around your variables

msg='{ "async": false, "key": "'$key'", "message": { "from_email": "'$from_email'", "from_name": "'$from_name'", "headers": { "Reply-To": "'$reply_to'" }, "auto_html": false, "return_path_domain": null, "subject": "'$2'", "text": "'$message_body'", "to": [ { "email": "'$1'", "type": "to" } ] } }'
# ..............................^^^^ no quotes around var ...........^^^^^^^^^^^...................^^^^^^^^^^...............................^^^^^^^^^...................................................................^^..............^^^^^^^^^^^^^.........................^^

Try this instead: any double quote in each variable is escaped.

escape_quotes() { echo "${1//"/"}"; }
msg=$(
    printf '{ "async": false, "key": "%s", "message": { "from_email": "%s", "from_name": "%s", "headers": { "Reply-To": "%s" }, "auto_html": false, "return_path_domain": null, "subject": "%s", "text": "%s", "to": [ { "email": "%s", "type": "to" } ] } }' 
        "$(escape_quotes "$key")"          
        "$(escape_quotes "$from_email")"   
        "$(escape_quotes "$from_name")"    
        "$(escape_quotes "$reply_to")"     
        "$(escape_quotes "$2")"            
        "$(escape_quotes "$message_body")" 
        "$(escape_quotes "$1")" 
)
链接地址: http://www.djcxy.com/p/48574.html

上一篇: 键盘Maestro Bash CURL脚本无变量但无法运行

下一篇: 如何在bash脚本中使用curl来发布JSON变量?