Python Forum
multiple values as parameters to POST command..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiple values as parameters to POST command..
#1
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
Reply
#2
(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
Reply
#3
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
Reply
#4
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
Reply
#5
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
Reply
#6
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
Reply
#7
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
Reply
#8
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'})
Reply
#9
can you check the type of response?
print(type(response))
Reply
#10
(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'>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  __init__() got multiple values for argument 'schema' dawid294 4 1,898 Jan-03-2024, 09:42 AM
Last Post: buran
  How to combine multiple column values into 1? cubangt 15 2,634 Aug-11-2022, 08:25 PM
Last Post: cubangt
  Substitue multiple substrings in one command Pavel_47 0 801 Jul-18-2022, 01:24 PM
Last Post: Pavel_47
  function accepts infinite parameters and returns a graph with those values edencthompson 0 812 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  Function - Return multiple values tester_V 10 4,321 Jun-02-2021, 05:34 AM
Last Post: tester_V
  How to input & output parameters from command line argument shantanu97 1 2,508 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Xlsxwriter: Create Multiple Sheets Based on Dataframe's Sorted Values KMV 2 3,442 Mar-09-2021, 12:24 PM
Last Post: KMV
  Looking for help in Parse multiple XMLs and update key node values and generate Out.. rajesh3383 0 1,847 Sep-15-2020, 01:42 PM
Last Post: rajesh3383
  Assigning multiple values using tuple sivacg 2 2,226 Aug-06-2020, 10:29 PM
Last Post: perfringo
  Function parameters and values as string infobound 1 1,729 Jul-24-2020, 04:28 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020