I am trying to write a Flask web app (hosted on Heroku) that will accept a datapoint POSTed to it from Xively.
The code below is meant to take the datapoint, modify it(add 2 to it), and then send it back to the xively feed:
import os
from flask import Flask
from flask import request
import xively
app = Flask(__name__)
@app.route('/', methods=['GET','POST'])
def take_xively_post_data():
if request.method == 'POST':
if not request.json or not 'current_value' in request.json:
abort(400)
else:
key = 'f5vezDTq4VphhXyCn......'
feedid = '21094....'
lev = request.json['current_value']
#the following code just adds 2 to the POSTed value and
#sends it back to xively
lev = lev + 2
client1 = xively.Client(key)
datastream = xively.Datastream(id="DatastreamID", current_value= lev)
return client1.put('/v2/feeds/'+feedid, data={'datastreams': [datastream]}
else:
return 'Hello'
But when Xively posts to the app's URL, nothing happens. (It should be sending the modified data back to the Xively feed.)
The Heroku logs indicate a 500 error:
2014-05-29T14:00:55.844275+00:00 heroku[router]: at=info method=POST path=/ host=....herokuapp.com request_id=... fwd="..." dyno=web.1 connect=1ms service=17ms status=500 bytes=454
I think there is a problem in the code but don't know what it is...
I have tried moving the client1.put(...) statement up and returning nothing as shown below but the same thing happens:
import os
from flask import Flask
from flask import request
import xively
app = Flask(__name__)
@app.route('/', methods=['GET','POST'])
def take_xively_post_data():
if request.method == 'POST':
if not request.json or not 'current_value' in request.json:
abort(400)
else:
key = 'f5vezDTq4VphhXyCn......'
feedid = '21094....'
lev = request.json['current_value']
lev = lev + 2
client1 = xively.Client(key)
datastream = xively.Datastream(id="DatastreamID", current_value= lev)
#moved the client1.put function up and returning nothing
client1.put('/v2/feeds/'+feedid, data={'datastreams': [datastream]}
return
else:
return 'Hello'
I have tried request.form() instead of request.json() but that doesn't seem to work either.
I would really appreciate it if anyone could point out the coding error.