401 Unauthorized with JSON - 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.

Related

Trying to Load A Message Payload from a JSON File in Bash to Send to Slack Channel

So I am trying to send a message to a specific slack channel using a secrets.json file to specify the payloads for both messages and the web-hook URL. I am able to post things to the message but it does not load the message as a payload even though the payload variable is set correctly. How can I load message payloads within the curl command I am specifying?
Here is the code:
richardbarret#1152-MBP  ~/Git/SalesforceCLI/Integration/Slack   master ●  ./automated_messages.sh  ✔  1488  10:48:42
https://hooks.slack.com/services/XXXXXXXXX/XXXXXXX/XXXXXX
Webhook Variable is Working.
invalid_payload% richardbarret#1152-MBP  ~/Git/SalesforceCLI/Integration/Slack   master ●  cat automated_messages.sh  ✔  1489  10:48:59
#!/bin/bash
# ===========================================================
# Created By: Richard Barrett
# Organization: Mirantis
# Department: Customer Success Operation
# Purpose: Send Message to Slack Channel
# Date: 03/20/2020
# ===========================================================
# System Variables
webhook_url=$(cat secrets.json | jq ".slack_config.slack_target_url" | tr -d \")
echo $webhook_url
printf "Webhook Variable is Working.\n"
message_1=$(cat secrets.json | jq ".slack_messages.message_1" | tr -d \")
message_2=$(cat secrets.json | jq ".slack_messages.message_2" | tr -d \")
message_3=$(cat secrets.json | jq ".slack_messages.message_3" | tr -d \")
# Use Messages in this command syntax
# Example
# curl -X POST -H 'Content-type: application/json' --data '{"text":"TEST TEXT BODY"}' $webhook_url
# General Message:
curl -X POST -H 'Content-type: application/json' --data '{"text":"$message_1"}' $webhook_url
# Messages for Handover:
#curl -X POST -H 'Content-type: application/json' --data '{"text":"Handovers: https://mirantis.my.salesforce.com/XXXXXXXXXXXXXXX"}' $webhook_url
# Message for All Change Requests:
#curl -X POST -H 'Content-type: application/json' --data '{"text":"All Change Requests: https://mirantis.my.salesforce.com/XXXXXXXXXXXX"}' $webhook_url
# Message for Change Requests in Ready to Execute
# curl -X POST -H 'Content-type: application/json' --data '{"text":"All CRs in Ready to Execute:"}' $webhook_url
I fixed the Invalid Payload because I forgot to use the "text": option within the --data so I know that is not the issue. When I run the above script all that get's posted it the following:
So I tried deleting the quotations around $message_1 and got the message:
Webhook Variable is Working.
invalid_payload%
I deleted the Webhook response because it has a slack token.
The webhook works, but the messages are not loading in the payload curl for the --data for some odd reason.
The secrets.json looks like this:
{
"slack_config": {
"slack_target_url": "https://hooks.slack.com/services/XXXXXXX/XXXXXXXXXXX/XXXXXXXXXXXXXXX"
},
"slack_messages": {
"message_1": "SLACK_MESSAGE_1 Testing Secrets Calls from secrets.json File",
"message_3": "SLACK_MESSAGE_3 Testing Secrets Calls from secrets.json File",
"message_2": "SLACK_MESSAGE_2 Testing Secrets Calls from secrets.json File"
}
}
So many thanks to one of my buddies who is an SRE, he was able to point me in the right direction. When trying to read something from a secrets.json it wasn't specifying the $message_1. But now if you replace the
--data '{"text":"$message_1"}'
with
--data "{\"text\":\"$message_1\"}"
you can then call the messages from within another file without exposing them.
This makes any links you may need to put in the message more secure as well as they are not hardcoded to the message.

How to get Curl header response?

I am trying to get the below response from my curl command. My command inserts data into a list and that works perfectly. However, I cannot seem to figure out how to get the below response.
curl -d "{"""name""":"""ME""","""id""":1}" -H "Content-Type: application/json" -X PUT http://localhost:8081/api/users
I have tried all of the following.
curl --trace, curl -v, curl -i, curl -w '%{response_code}'
DEBUG 2018-11-26 16:15:24,853 [http.listener.06 SelectorRunner] [event: ]
org.mule.service.http.impl.service.HttpMessageLogger.HTTP_Listener_config: LISTENER
PUT /api/users HTTP/1.1
Host: localhost:8081
User-Agent: curl/7.54.0
Accept: */*
Content-type:application/json
Content-Length: 18
{"name":"ME","id":1}
DEBUG 2018-11-26 16:15:24,861 [[MuleRuntime].cpuLight.03:
[troubleshooting_myapi].troubleshooting_myapiFlow2.CPU_LITE #541c15d] [event:
0-a0773d60-f1af-11e8-9c18-70886b824af4]
org.mule.service.http.impl.service.HttpMessageLogger.HTTP_Listener_config: LISTENER
HTTP/1.1 200
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 26 Nov 2018 19:15:24 GMT
a9
[
{
"name": "ME",
"id": 1
}
]
Try with -D -.
This is what curl -h says about header options
-D, --dump-header <filename> Write the received headers to <filename>
The - instead of filename indicates the stdout.
curl -D - -d "{"""name""":"""ME""","""id""":1}" -H "Content-Type: application/json" -X PUT http://localhost:8081/api/users

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"

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);
?>