How do I construct a JSON message to send with Amazon-SES? - json

I'm running into the following error when I try to send an email via Amazon-SES:
Error parsing parameter '--raw-message': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
I don't understand where I am messing up the JSON message...
This is the bash script I am using:
echo '{\"Data\":
"Subject:'${SUBJECT}'\\n
MIME-Version: 1.0\n
Content-type: Multipart/Mixed;
boundary=\"NextPart\"\\n\\n--NextPart\\n
Content-Type: text/plain\\n\\n'${BODY}'
\\n\\n--NextPart
\\nContent-Type:'${ATTACHMENT_TYPE}';
\nContent-Disposition: attachment;
filename=\"'${ATTACHMENT_FILE_NAME}'\"\\n
\\n'$(cat ./${ATTACHMENT_FILE_TO_READ_FROM_DISK})'
\\n--NextPart--"}' > "$tmp_message"
aws ses send-raw-email --region us-east-1 --from sender#gmail.com --destination file://$tmp_destination --raw-message file://"$tmp_message"

Use jq to verify your json is fine
#! /bin/bash
echo '{\"Data\":
"Subject:'${SUBJECT}'\\n
MIME-Version: 1.0\n
Content-type: Multipart/Mixed;
boundary=\"NextPart\"\\n\\n--NextPart\\n
Content-Type: text/plain\\n\\n'${BODY}'
\\n\\n--NextPart
\\nContent-Type:'${ATTACHMENT_TYPE}';
\nContent-Disposition: attachment;
filename=\"'${ATTACHMENT_FILE_NAME}'\"\\n
\\n'$(cat ./${ATTACHMENT_FILE_TO_READ_FROM_DISK})'
\\n--NextPart--"}' > "$tmp_message"
if jq < "$tmp_message"; then
aws ses send-raw-email --region us-east-1 --from sender#gmail.com --destination file://"$tmp_destination" --raw-message file://"$tmp_message"
else
echo "ERROR" >&2
fi

Related

Powershell | cURL | invalid JSON body in the request

i'm trying to connect to my server and pass some info via API calls by powershell, but always get JSON error for below command, i'm not able to figure out the issue, i hope if anyone face the same can help..
original data:
"<If \"%{HTTP:connection} =~ /close/i \">
RequestHeader set connection close
</If>
<ElseIf \"%{HTTP:connection} =~ /keep-alive/i \">
RequestHeader set connection keep-alive
</ElseIf>
<Else>
RequestHeader set connection close
</Else>"
the Curl command i'm trying:
curl.exe -sk https://10.1.2.209/api/v1/httpd -u test:test23 -H "Content-Type: application/json" -X PATCH -d \ '{\"include\" : \"<If "%{HTTP:connection} =~ /close/i ">\nRequestHeader set connection close\n</If>\n<ElseIf \"%{HTTP:connection} =~ /keep-alive/i \">\nRequestHeader set connection keep-alive\n</ElseIf>\n<Else>\n RequestHeader set connection close\n</Else>\"}'
error i'm getting
{"code":400,"message":"Found invalid JSON body in the request."

Use Variable in CURL GET

I feel that I am just missing something very silly and not quoting/escaping something as I should, but I've been reading and testing for a solid amount of time now, and just can't get this to work.
I have a CSV file with data like the below:
TESTING.csv
17A3120UAXF-AA002771,9911017093
S150Y52157201,9911008933
17A3120UAXF-AA004545,9911016519
S170Y13084226,9911024365
S160Y45021270,9911018486
For the first part of my script, I need to read the second variable (ie. the items starting with "99110...")
I am parsing this data into a CURL while loop to sequence through all lines in the file. My bash script is like the below at this time:
#!/bin/bash
while IFS=, read -r device account; do
echo "device : $device"
echo "account : $account"
curl -X GET --header "Content-Type: application/json" --user username:password --verbose 'https://www.website.com:8444/api/subscriber?account=$account'
done < TESTING.csv
The issue that I'm running into is that while the "echo" statements are able to correctly pull/show the variable that I'm wanting to pass, this same information is not being passed into my CURL commands. When I run my script, the output is like the below:
device : 17A3120UAXF-AA002771
account : 9911017093
* About to connect() to www.website.com port 8444 (#0)
* Trying 8.8.8.8...
* Connected to www.website.com (8.8.8.8) port 8444 (#0)
* Server auth using Basic with user 'username'
> GET /api/subscriber?account=$account HTTP/1.1
> Authorization: Basic madeUpJunkAndNumbers12345==
> User-Agent: curl/7.29.0
> Host: www.website.com:8444
> Accept: */*
> Content-Type: application/json
>
< HTTP/1.1 404 Not Found
< Content-Type: application/json
< Content-Length: 0
<
* Connection #0 to host www.website.com left intact
^C
You are using single quotes int the URL, so the variables don't get expanded. Use double quotes:
curl -X GET --header "Content-Type: application/json" --user username:password --verbose "https://www.website.com:8444/api/subscriber?account=$account"

401 Unauthorized with JSON

I got an error when i run the bash script below.
pi#raspberrypi:~/dev-domoticz/scripts $ ./dht_22.sh
21.2
48.2
HTTP/1.1 401 Unauthorized
Content-Length: 91
Content-Type: text/html
Set-Cookie: SID=none; HttpOnly; Expires=Thu, 01 Jan 1970 00:00:00 GMT
<html><head><title>Unauthorized</title></head><body><h1>
This script reads the DHT22 chip for temp and humidity dht_22.sh and communicate with the Domoticz server with json. So i can see the current temp/humi on my Domoticz server:
#!/bin/sh
# Domoticz server
SERVER="10.0.0.110:8080"
# DHT IDX
DHTIDX="4"
# DHTPIN
DHTPIN="4"
# TEMP FILE
TMPFILE="/var/tmp/temp.txt"
cpt=0
while [ $cpt -lt 6 ]
do
TEMP=""
sleep 5
sudo nice -20 /home/pi/dev-domoticz/scripts/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 $DHTPIN > /var/tmp/temp.t$
#TEMP=$(cat /var/tmp/temp.txt | grep "Temp" | awk '{ print $3 }')
#TEMP=$(cat /var/tmp/temp.txt | grep "Temp")
TEMP=$(awk ' /Temp/ {print substr ($0,6,4)}' /var/tmp/temp.txt)
HUM=$(awk ' /Humidity/ {print substr ($0,22,4)}' /var/tmp/temp.txt)
echo $TEMP
echo $HUM
# Send data
curl -s -i -H "Accept: application/json" "http://10.0.0.110:8080/json.htm?type=command&param=udevice&idx=$DHTIDX&nv$
TEMP=""
HUM=""
exit 0
cpt=$(($cpt+1))
done
exit 1
When i just run the json line in a browser, i also receive a 401 Unauthorized error.
I gues i have to enter some login information, so i also tryed something like this:
http://10.0.0.110:8080/json.htm?username=test=&password=test=&type=command&param=udevice&idx=4&nvalue=0&svalue=21;40;2
But still the 401 error.
Can some one help me out?
Well, cost me some time but it work now.
Your solution was correct Viktor Khilin but my Domoticz software gave me some problems.
The solution was this:
curl -s -i -H "Accept: application/json" "http://10.0.0.110:8080/json.htm?&username=test=&password=test=&type=command&param=udevice&idx=$DHTIDX&nvalue=0&svalue=$TEMP;$HUM;2"
Thanks for helping me out.

Error Accessing Data Volumes for MobileFirst Platform Analytics Container on Bluemix

I am creating MobileFirst container groups on Bluemix. My issue is with creating the Analytics container group. I have edited the appropriate .properties files, and in args/startanalyticsgroup.properties, I have set ENABLE_ANALYTICS_DATA_VOLUME=Y and ANALYTICS_DATA_VOLUME_NAME=mfp_analytics_$ANALYTICS_CONTAINER_GROUP_NAME.
When I execute the ./startanalyticsgroup.sh args/startanalyticsgroup.properties the volume gets created successfully. However, as the script continues to process it throws an error saying the data volume cannot be retrieved. The error message is below. Thank you in advance for any assistance you can provide.
./startanalyticsgroup.sh args/startanalyticsgroup.properties
Arguments :
-----------
ANALYTICS_IMAGE_NAME : registry.ng.bluemix.net/dockercontainerrepo/mfpanalytics71
ANALYTICS_CONTAINER_GROUP_NAME : mfpAnalytics
ANALYTICS_CONTAINER_GROUP_MIN : 1
ANALYTICS_CONTAINER_GROUP_MAX : 2
ANALYTICS_CONTAINER_GROUP_DESIRED : 1
ENABLE_AUTORECOVERY : Y
ANALYTICS_CONTAINER_GROUP_HOST : mfpanalytics
ANALYTICS_CONTAINER_GROUP_DOMAIN : mybluemix.net
SERVER_MEM : 2048
TRACE_SPEC :
MAX_LOG_FILES :
MAX_LOG_FILE_SIZE :
MFPF_PROPERTIES :
ENABLE_VOLUME : N
ENABLE_ANALYTICS_DATA_VOLUME : Y
ANALYTICS_DATA_VOLUME_NAME : mfp_analytics_mfpAnalytics
ANALYTICS_DATA_DIRECTORY : /analyticsData
The volume mfp_analytics_mfpAnalytics will be created to store analytics data.
OK
Volume 'mfp_analytics_mfpAnalytics' was created.
Starting the analytics container group : mfpAnalytics
Executing command : cf ic group create --name mfpAnalytics -n mfpanalytics -d mybluemix.net -m 2048 --min 1 --max 2 --desired 1 -p 9080 --auto -v mfp_analytics_mfpAnalytics:/analyticsData -e ANALYTICS_DATA_DIRECTORY=/analyticsData -e ANALYTICS_TRACE_LEVEL=*~info -e ANALYTICS_MAX_LOG_FILES=5 -e ANALYTICS_MAX_LOG_FILE_SIZE=20 registry.ng.bluemix.net/dockercontainerrepo/mfpanalytics71
REQUEST: [2016-05-19T13:43:03-04:00]
POST /UAALoginServerWAR/oauth/token HTTP/1.1
Host: login.ng.bluemix.net
Accept: application/json
Authorization: [PRIVATE DATA HIDDEN]
Content-Type: application/x-www-form-urlencoded
User-Agent: go-cli 6.17.0+5d0be0a / darwin
grant_type=refresh_token&refresh_token=eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI5ZTgzZmQyZS00Y2MxLTQ4OWUtODA1Yi1lYWNjMzcyOWYzNDEtciIsInN1YiI6ImZlMzU4Y2IwLWNmYjYtNDI3MC05NmMyLTYyNzVmNTA5NjI1OCIsInNjb3BlIjpbIm9wZW5pZCIsImNsb3VkX2NvbnRyb2xsZXIucmVhZCIsInBhc3N3b3JkLndyaXRlIiwiY2xvdWRfY29udHJvbGxlci53cml0ZSJdLCJpYXQiOjE0NjM2NzkxNjYsImV4cCI6MTQ2NjI3MTE2NiwiY2lkIjoiY2YiLCJjbGllbnRfaWQiOiJjZiIsImlzcyI6Imh0dHBzOi8vdWFhLm5nLmJsdWVtaXgubmV0L29hdXRoL3Rva2VuIiwiemlkIjoidWFhIiwiZ3JhbnRfdHlwZSI6InBhc3N3b3JkIiwidXNlcl9uYW1lIjoibG9uZ3RAdXMuaWJtLmNvbSIsIm9yaWdpbiI6InVhYSIsInVzZXJfaWQiOiJmZTM1OGNiMC1jZmI2LTQyNzAtOTZjMi02Mjc1ZjUwOTYyNTgiLCJyZXZfc2lnIjoiZmI2ZjZkZmEiLCJhdWQiOlsiY2YiLCJvcGVuaWQiLCJjbG91ZF9jb250cm9sbGVyIiwicGFzc3dvcmQiXX0.QXXRHERyg5JbAg2l8Jx7O7e-JRC4vgwmrgDMWPjm2LQ&scope=
RESPONSE: [2016-05-19T13:43:03-04:00]
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Cache-Control: no-cache, no-store, max-age=0, must-revalidate,no-store
Connection: Keep-Alive
Content-Type: application/json;charset=UTF-8
Date: Thu, 19 May 2016 17:43:03 GMT
Expires: 0
Pragma: no-cache,no-cache
Server: Apache-Coyote/1.1
X-Backside-Transport: OK OK,OK OK
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Global-Transaction-Id: 1067898045
X-Powered-By: Servlet/3.1
X-Xss-Protection: 1; mode=block
699
{"access_token":"[PRIVATE DATA HIDDEN]","token_type":"bearer","refresh_token":"[PRIVATE DATA HIDDEN]","expires_in":43200,"scope":"cloud_controller.read password.write cloud_controller.write openid","jti":"cd2c3f32-be0e-4b35-acb0-058f0e49fac1"}
0
FAILED
"{\n \"code\": \"IC5051E\", \n \"description\": \"The image registry.ng.bluemix.net/mfp_analytics_mfpAnalytics:/analyticsData could not be retrieved. Verify that the image ID or name is correct.\", \n \"incident_id\": \"740-1463679783.961-19195817\", \n \"name\": \"ImageNotFound\", \n \"rc\": \"404\", \n \"type\": \"Infrastructure\"\n}"
The latest version of 'cf' (6.17+) is now pulling out the -v parameter for its own usage.
Use --volume instead of -v should fix it:
e.g.:
cf ic group create --name mfpAnalytics -n mfpanalytics -d mybluemix.net -m 2048 --min 1 --max 2 --desired 1 -p 9080 --auto --volume mfp_analytics_mfpAnalytics:/analyticsData -e ANALYTICS_DATA_DIRECTORY=/analyticsData -e ANALYTICS_TRACE_LEVEL=*~info -e ANALYTICS_MAX_LOG_FILES=5 -e ANALYTICS_MAX_LOG_FILE_SIZE=20 registry.ng.bluemix.net/dockercontainerrepo/mfpanalytics71
Or, simpler, switch back to cf version 6.16 for now.

JSON Post data empty - Linux curl Command

I am trying to post json data to php file using linux curl command, (Lamp Server)
$ curl -V -H "Content-Type: application/json" -X POST -d '{"id": "123"}'
http://localhost/crm/UpdateUser.php
In UpdateUser.php,
<?php echo var_dump ($_POST);?>
OUTPUT:
[ec2-user#ip-10-35-1-181 ~]$ curl -v -H "Content-Type: application/json" -X POST -d '{"id": "123"}' http://viacrm.odema.net/crm/UpdateUser.php
* Hostname was NOT found in DNS cache
* Trying 54.217.206.217...
> POST /crm/UpdateUser.php HTTP/1.1
> User-Agent: curl/7.36.0
> Host: 192.168.1.16
> Accept: */*
> Content-Type: application/json
> Content-Length: 13
>
* upload completely sent off: 13 out of 13 bytes
< HTTP/1.1 200 OK
< Date: Mon, 16 Jun 2014 12:25:00 GMT
* Server Apache/2.2.27 (Amazon) is not blacklisted
< Server: Apache/2.2.27 (Amazon)
< X-Powered-By: PHP/5.3.28
< Content-Length: 13
< Connection: close
< Content-Type: text/html; charset=UTF-8
<
array(0) {
}
* Closing connection 0
Always the Post data shows empty, I even tried to use "ACCEPT: application/json", still same problem. Please can anyone guide this ?
$_POST only contains the results of decoding an application/x-www-form-urlencoded request. You need to read the raw request body. If you have the always_populate_raw_post_data configuration directive turned on, then the raw body will be in $HTTP_RAW_POST_DATA; otherwise you can obtain it by reading from the php://input stream.
Instead of $_POST try it:
<?php
print($HTTP_RAW_POST_DATA);
?>