title: Call rest api to push data to kinesis using python
body: <p>I am a newbie in python and aws.I dont know much how to ask questions in stackoverflow.</p>
  
  <p>Please do not block me.</p>
  
  <p>I am trying to send a HTTP Post Request to put record into Amazon Kinesis Stream.</p>
  
  <p>I have created a stream mystream in kinesis. I use method post.</p>
  
  <p>I tried the following link to set up gateway api and it worked fine.</p>
  
  <p><a href="https://docs.aws.amazon.com/kinesis/latest/APIReference/API_CreateStream.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/kinesis/latest/APIReference/API_CreateStream.html</a></p>
  
  <p>I am trying to do it with python code using requests.</p>
  
  <p>But i am getting the below mentioned error:</p>
  
  <p>The following is my code:</p>
  
  <pre><code>import sys, os, base64, datetime, hashlib, hmac 
  import requests # pip install requests
  
  # ************* REQUEST VALUES *************
  method = 'POST'
  service = 'kinesis'
  host = 'kinesis.eu-west-1.amazonaws.com'
  region = 'eu-west-1'
  endpoint = 'https://kinesis.eu-west-1.amazonaws.com'
  
  content_type = 'application/x-amz-json-1.1'
  amz_target = 'Kinesis_20181114.PutRecord'
  request_parameters = '{'
  request_parameters += '"StreamName": mystream,'
  request_parameters += '"Data":  + base64.b64encode(test) + ,'
  request_parameters += '"PartitionKey": 1234 '
  request_parameters += '}'
  
  
  # Key derivation functions. See:
  # http://docs.aws.amazon.com/general/latest/gr/signature-v4- 
  examples.html#signature-v4-examples-python
  def sign(key, msg):
  return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
  
  def getSignatureKey(key,datestamp,region,service):
  kDate = sign(('AWS4' +key ).encode('utf-8'), datestamp)
  kRegion = sign(kDate,region)
  kService = sign(kRegion,service)
  kSigning = sign(kService, 'aws4_request')
  return kSigning
  
  # Read AWS access key from env. variables or configuration file. Best 
  practice is NOT
  # to embed credentials in code.
  with open ('C:\\Users\\Connectm\\Desktop\\acesskeyid.txt') as f:
         contents = f.read().split('\n')
         access_key = contents[0]
         secret_key = contents[1]
  
  
   if access_key is None or secret_key is None:
   print('No access key is available.')
   sys.exit()
  
  # Create a date for headers and the credential string
  t = datetime.datetime.utcnow()
  amzdate = t.strftime('%Y%m%dT%H%M%SZ')
  datestamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope
  
  canonical_uri = '/'
  
  canonical_querystring = ''
  
  canonical_headers = 'content-type:' + content_type + '\n' + 'host:' + host + 
  '\n' + 'x-amz-date:' + amzdate + '\n' + 'x-amz-target:' + amz_target + '\n'
  
  signed_headers = 'content-type;host;x-amz-date;x-amz-target'
  
  payload_hash = hashlib.sha256(request_parameters).hexdigest()
  
  canonical_request = method + '\n' + canonical_uri + '\n' + 
  canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + 
  '\n' + payload_hash
  
  algorithm = 'AWS4-HMAC-SHA256'
  credential_scope = datestamp + '/' + region + '/' + service + '/' + 
  'aws4_request'
   string_to_sign = algorithm + '\n' +  amzdate + '\n' +  credential_scope + 
  '\n' +  hashlib.sha256(canonical_request).hexdigest()
  
  signing_key = getSignatureKey(secret_key, datestamp, region, service)
  
   signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), 
  hashlib.sha256).hexdigest()
  
   authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + 
  credential_scope + ', ' +  'SignedHeaders=' + signed_headers + ', ' + 
  'Signature=' + signature
  print authorization_header;
  
  
  headers = {'Content-Type':content_type,
         'X-Amz-Date':amzdate,
         'X-Amz-Target':amz_target,
         'Authorization':authorization_header}
   # ************* SEND THE REQUEST *************
  print '\nBEGIN REQUEST++++++++++++++++++++++++++++++++++++'
  print 'Request URL = ' + endpoint
  r = requests.post(endpoint, data=request_parameters, headers=headers)
  print '\nRESPONSE++++++++++++++++++++++++++++++++++++'
  print 'Response code: %d\n' % r.status_code
  print r.text
  </code></pre>
  
  <p>The following error i am getting</p>
  
  <p>AWS4-HMAC-SHA256 Credential=AKIAI5C357A6YSKQFXEA/20181114/eu-west- 
   1/kinesis/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz- 
   target, 
   Signature=1d7d463e77beaf86930806812188180db9cc7cff082663ad547f647a9c6d545a</p>
  
  <p>BEGIN REQUEST++++++++++++++++++++++++++++++++++++
   Request URL = <a href="https://kinesis.eu-west-1.amazonaws.com" rel="nofollow noreferrer">https://kinesis.eu-west-1.amazonaws.com</a></p>
  
  <p>RESPONSE++++++++++++++++++++++++++++++++++++
   Response code: 400</p>
  
  <p>{"__type":"SerializationException"}</p>
  
  <p>Please can someone explain me how i can rectify the above error?</p>
  
  <p>Is the code connecting to the stream?Is there a problem regarding 
   serialization of data?</p>