title: Python 3 Gzip json for post on an API
body: <p>Having a bit of trouble getting a gzipped JSON payload with python 3</p>
  
  <pre><code>def post_data ( data ) :
      method_name = json.loads(data)['subject'][4][0][1]
      instance = json.loads(data)['serialNumber']
      timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
      utc_now = datetime.datetime.utcnow().strftime("%m-%d-%Y %H:%M:%S")
      not_after = datetime.datetime.strptime(json.loads(data)['notAfter'], "%b %d %H:%M:%S %Y GMT" ).strftime("%m-%d-%Y %H:%M:%S")
      days =  datetime.datetime.strptime(not_after, "%m-%d-%Y %H:%M:%S")-\
              datetime.datetime.strptime(utc_now, "%m-%d-%Y %H:%M:%S")
      values = days.days
  
  raw_json = '''{"MetricReports":[{
                     "Metadata":{
                       "MinPeriod":"FiveMinute",
                       "MaxPeriod":"FiveMinute"
                     },
                     "Namespace":"Schema/Service",
                     "Metrics":[{
                         "Dimensions":{
                           "DataSet":"NONE",
                           "Marketplace":"PDX",
                           "HostGroup":"ALL",
                           "Host":"host.com",
                           "ServiceName":"Server",
                           "MethodName":"%s",
                           "Client":"ALL",
                           "MetricClass":"instance",
                           "Instance":"%s"
                         },
                         "MetricName":"daysToExpiry",
                         "Timestamp":"%s",
                         "Type":"MetricsLine",
                         "Unit":"None",
                         "Values":%s
                       }
                     ]}
                   ]}''' %(method_name, instance, timestamp, values)
  
  
  headers = {'content-type' : 'application/x-gzip'}
  putUrl = 'http://api-pdx.com/'
  session = boto3.Session()
  credentials = session.get_credentials()
  region = 'us-west-2'
  service = 'monitor-api'
  auth = AWS4Auth(
              credentials.access_key,
              credentials.secret_key,
              region,
              service,
              session_token = credentials.token)
  
  r = requests.post(url = putUrl, json = gzip.compress(raw_json) , auth = auth, headers = headers, verify=False)
  print(r.content)
  
  get_data()
  post_data(data)
  </code></pre>
  
  <p>I need to compress the raw_json because the API is expecting a gzipped attachment.  The API is spitting back "The HTTP request is invalid. Reason: Invalid JSON attachment:Not in GZIP format" if I attempt to send the JSON as is.</p>
  
  <p>I tried the gzip.compress, but it says : memoryview: a bytes-like object is required, not 'str'</p>
  
  <p>so I tried gzip.compress(json.dumps(raw_json).encode('utf-8')) and that says Object of type bytes is not JSON serializable</p>