Python Forum

Full Version: multiple values as parameters to POST command..
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi Experts,

I need some help.

What I actually need is to post some server status to slack channel with python
The below is the example code i am trying
import json
import requests

# Set the webhook_url to the one provided by Slack when you create the webhook at https://my.slack.com/services/new/incoming-webhook/
webhook_url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
slack_data = {'text': "Hello from python"}

response = requests.post(
    webhook_url, data=json.dumps(slack_data),
    headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
    raise ValueError(
        'Request to slack returned an error %s, the response is:\n%s'
        % (response.status_code, response.text)
    )
Though above code is working fine. But I want to pass multiple values in slack_data as above example is having only one data value is being posted on slack. But I want to mention multiple values at line no 6
I tried different methods and ended up with some errors like " too many values to unpack, Json not serializable etc

Can you some one guide me or assist me to get multiple values to be posted on slack channel

Thank you very munch in advance

best regards
ASP
(Feb-19-2018, 09:20 AM)onenessboy Wrote: [ -> ]But I want to pass multiple values in slack_data as above example is having only one data value is being posted on slack. But I want to mention multiple values at line no 6
I tried different methods and ended up with some errors like " too many values to unpack, Json not serializable etc


Please, post the exact code you tried and full traceback of errors you get (in error tags).
It looks like you are not able to 'produce' valid json, i.e. the problem is not slack/POST related
Hi Thanks for your reply,

sorry, i am replying bit late and trying to put out various methods and failing :(

The actual context is i am trying to get some linux server status and posting to slack channel

In original post I have given the sample which is base code which i am trying to build correct code for my context

This is original code
from __future__ import print_function

import boto3
import json
import logging
import os
logging.basicConfig()

from urllib2 import Request, urlopen, URLError, HTTPError

# configuration

# The Slack channel to send a message to stored in the slackChannel environment variable
SLACK_CHANNEL = "#XXXXXXXXXX"
# Add the webhook URL from Slack below
HOOK_URL = "https://hooks.slack.com/services/xxxxx/xxxx/xxxxx"

response = conn.get_all_instances()
MESSAGE = response
# Setting up logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

slack_message = {
    'channel': SLACK_CHANNEL,
    'text': MESSAGE}

req = Request(HOOK_URL, json.dumps(slack_message))
try:
    response = urlopen(req)
    response.read()
    logger.info("Message posted to %s", slack_message['channel'])
except HTTPError as e:
    logger.error("Request failed: %d %s", e.code, e.reason)
except URLError as e:
    logger.error("Server connection failed: %s", e.reason)
So in above code line number 18 the variable "response" actually returns in below format
{
    "Reservations": [
        {
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "PublicDnsName": "",
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
                        "Code": "Client.UserInitiatedShutdown"
                    },
                    "State": {
                        "Code": 80,
                        "Name": "stopped"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "2018-02-08T10:37:36.000Z",
                    "PrivateIpAddress": "xx.xx.xx.xxx",
                    "ProductCodes": [],
                    "VpcId": "xxxxxx",
                    "StateTransitionReason": "User initiated (2018-02-08 11:13:34 GMT)",
                    "InstanceId": "xxxxxxx",
                    "EnaSupport": true,
                    "ImageId": "xxxxx",
                    "PrivateDnsName": "xxxxxxxx",
                    "KeyName": "xxxxxx",
                    "SecurityGroups": [
                        {
                            "GroupName": "xxxxxxxx",
                            "GroupId": "xx-xxxx"
                        }
                    ],
                    "ClientToken": "",
                    "SubnetId": "xxxx-xxxxx",
                    "InstanceType": "xx.xxx",
                    "NetworkInterfaces": [
so i want to post this information to slack channel in under stable way for readers

Hope I am giving right info.. can you please help..where i am doing it wrong...

The above code says json is not serializable error

Regargs
ASP
are you sure this is full response? It is json, but not full, are you sure you receive the full response? also what is conn?
finally better use requests module, not urllib2
I'm looking at boto.ec2.connection.get_all_instances() documentation
It says it returns a list of Reservations, while what you post looks like dict/json
sorry i did not post entire response..

While conn is environment variable which is having connecting details

here it is
{
    "Reservations": [
        {
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "PublicDnsName": "",
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
                        "Code": "Client.UserInitiatedShutdown"
                    },
                    "State": {
                        "Code": 80,
                        "Name": "stopped"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "2018-02-08T10:37:36.000Z",
                    "PrivateIpAddress": "xx.xx.x.xxx",
                    "ProductCodes": [],
                    "VpcId": "xx-xxxx",
                    "StateTransitionReason": "User initiated (2018-02-08 11:13:34 GMT)",
                    "InstanceId": "xxxxxxxx",
                    "EnaSupport": true,
                    "ImageId": "xx-xxxxx",
                    "PrivateDnsName": "xxxxxxx",
                    "KeyName": "xxxxx",
                    "SecurityGroups": [
                        {
                            "GroupName": "xxxxxx",
                            "GroupId": "xxxxxxx"
                        }
                    ],
                    "ClientToken": "",
                    "SubnetId": "xxxx-xxxx",
                    "InstanceType": "t2.micro",
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use",
                            "MacAddress": "xxxxxxx",
                            "SourceDestCheck": true,
                            "VpcId": "xxxxx",
                            "Description": "Primary network interface",
                            "NetworkInterfaceId": "xxxx",
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "xxxxxxx",
                                    "Primary": true,
                                    "PrivateIpAddress": "xx.xx.xx.xxx"
                                }
                            ],
                            "PrivateDnsName": "xxxxxx",
                            "Attachment": {
                                "Status": "xxx",
                                "DeviceIndex": 0,
                                "DeleteOnTermination": true,
                                "AttachmentId": "xxxxxx",
                                "AttachTime": "2017-12-28T17:24:50.000Z"
                            },
                            "Groups": [
                                {
                                    "GroupName": "xx",
                                    "GroupId": "xx-xxxx"
                                }
                            ],
                            "Ipv6Addresses": [],
                            "OwnerId": "xxxxxxx",
                            "SubnetId": "subnet-xxxxx",
                            "PrivateIpAddress": "xx.xx.x.x"
                        }
                    ],
                    "SourceDestCheck": true,
                    "Placement": {
                        "Tenancy": "default",
                        "GroupName": "",
                        "AvailabilityZone": "xx-xx-xx"
                    },
                    "Hypervisor": "xen",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/xx/xx",
                            "Ebs": {
                                "Status": "attached",
                                "DeleteOnTermination": true,
                                "VolumeId": "vol-xxxxxxxx",
                                "AttachTime": "2017-12-28T17:24:51.000Z"
                            }
                        }
                    ],
                    "Architecture": "x86_64",
                    "RootDeviceType": "ebs",
                    "RootDeviceName": "/dev/xvda",
                    "VirtualizationType": "hvm",
                    "Tags": [
                        {
                            "Value": "xxxx",
                            "Key": "Name"
                        }
                    ],
                    "AmiLaunchIndex": 0
                }
            ],
            "ReservationId": "00000f4510a2551",
            "Groups": [],
            "OwnerId": "xxxxxxxx"
        }
        
}
This is the traceback

Traceback (most recent call last):
File "/home/user/script/ABSlack1.py", line 43, in <module>
req = Request(HOOK_URL, json.dumps(response))
File "/usr/lib64/python2.7/json/__init__.py", line 244, in dumps
return _default_encoder.encode(obj)
File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib64/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Reservation:r-00000f4510a2551 "not JSON serializable
note that:
1) I think you miss ] on line 108
2) I assume resp is string

try

resp = """{
    "Reservations": [
        {
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "PublicDnsName": "",
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
                        "Code": "Client.UserInitiatedShutdown"
                    },
                    "State": {
                        "Code": 80,
                        "Name": "stopped"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "2018-02-08T10:37:36.000Z",
                    "PrivateIpAddress": "xx.xx.x.xxx",
                    "ProductCodes": [],
                    "VpcId": "xx-xxxx",
                    "StateTransitionReason": "User initiated (2018-02-08 11:13:34 GMT)",
                    "InstanceId": "xxxxxxxx",
                    "EnaSupport": true,
                    "ImageId": "xx-xxxxx",
                    "PrivateDnsName": "xxxxxxx",
                    "KeyName": "xxxxx",
                    "SecurityGroups": [
                        {
                            "GroupName": "xxxxxx",
                            "GroupId": "xxxxxxx"
                        }
                    ],
                    "ClientToken": "",
                    "SubnetId": "xxxx-xxxx",
                    "InstanceType": "t2.micro",
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use",
                            "MacAddress": "xxxxxxx",
                            "SourceDestCheck": true,
                            "VpcId": "xxxxx",
                            "Description": "Primary network interface",
                            "NetworkInterfaceId": "xxxx",
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "xxxxxxx",
                                    "Primary": true,
                                    "PrivateIpAddress": "xx.xx.xx.xxx"
                                }
                            ],
                            "PrivateDnsName": "xxxxxx",
                            "Attachment": {
                                "Status": "xxx",
                                "DeviceIndex": 0,
                                "DeleteOnTermination": true,
                                "AttachmentId": "xxxxxx",
                                "AttachTime": "2017-12-28T17:24:50.000Z"
                            },
                            "Groups": [
                                {
                                    "GroupName": "xx",
                                    "GroupId": "xx-xxxx"
                                }
                            ],
                            "Ipv6Addresses": [],
                            "OwnerId": "xxxxxxx",
                            "SubnetId": "subnet-xxxxx",
                            "PrivateIpAddress": "xx.xx.x.x"
                        }
                    ],
                    "SourceDestCheck": true,
                    "Placement": {
                        "Tenancy": "default",
                        "GroupName": "",
                        "AvailabilityZone": "xx-xx-xx"
                    },
                    "Hypervisor": "xen",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/xx/xx",
                            "Ebs": {
                                "Status": "attached",
                                "DeleteOnTermination": true,
                                "VolumeId": "vol-xxxxxxxx",
                                "AttachTime": "2017-12-28T17:24:51.000Z"
                            }
                        }
                    ],
                    "Architecture": "x86_64",
                    "RootDeviceType": "ebs",
                    "RootDeviceName": "/dev/xvda",
                    "VirtualizationType": "hvm",
                    "Tags": [
                        {
                            "Value": "xxxx",
                            "Key": "Name"
                        }
                    ],
                    "AmiLaunchIndex": 0
                }
            ],
            "ReservationId": "00000f4510a2551",
            "Groups": [],
            "OwnerId": "xxxxxxxx"
        }
    ]
}"""
import json
slack_msg = json.loads(resp)
slack_msg['channel'] = SLACK_CHANNEL
slack_json = json.dumps(slack_msg)
req = Request(HOOK_URL, slack_json)
again better use requests, instead of urllib2
Hi,

tried as you suggested with requests.post instead of urllib2. following is the tracebak
and sorry while pasting i missed that ] which you have mentioned..

Traceback (most recent call last):
File "/home/user/script/ABSlack1.py", line 39, in <module>
slack_msg = json.loads(response)
File "/usr/lib64/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

This is what code change which i tried as per you suggestions

slack_msg = json.loads(response)
slack_msg['channel'] = SLACK_CHANNEL
slack_json = json.dumps(slack_msg)
#req = Request(HOOK_URL, slack_json)
response = requests.post(
    webhook_url, data=slack_json,
    headers={'Content-Type': 'application/json'})
can you check the type of response?
print(type(response))
(Feb-20-2018, 02:03 PM)buran Wrote: [ -> ]can you check the type of response?
print(type(response))

Hi Buran,

just checked the type of response. below is the response

<class 'boto.resultset.ResultSet'>
Pages: 1 2