Hello everyone,
I'm trying to use a very simple python function:
Handler.py
from flask import request, redirect
import redis
redisConnection = redis.StrictRedis(host='redis.guestbook', port=6379, db=0)
def handle(req):
# Read the item from POST params, add it to redis, and redirect
# back to the list
# if 'text' in event['data']:
item = req['data']['text']
redisConnection.rpush('guestbook', item)
#r = redirect('/', code=303)
# return 'added'
# return 'not added'
but i have the following error when trying to call it:
curl -d '{"text": "hello"}' http://172.16.52.11:31112/function/add
exit status 1
Traceback (most recent call last):
File "index.py", line 15, in
ret = handler.handle(st)
File "/home/app/function/handler.py", line 11, in handle
item = request['data']['text']
TypeError: string indices must be integers, not str
Using Kubernetes (Faas-netes)
Any help please.
Thank you
Hello @white-cloud. Its really difficult to tell from the detail provided, but this does look alike a python error. Have you tried validating the code outside of openfaas?
Is your code available in a repo anywhere? Can you provide enough detail to enable someone else to reproduce, please?
Derek add label: support
Hello @rgee0 Thank you for your answer
actually this is the code that i'm trying to execute in openfaas.
https://github.com/fission/fission/tree/master/examples/python/guestbook
I will really appreciate your help.
Thank you again :)
I tried a lot to change the code so many time to not getting this errors, i fixed some , but always having that error when trying the call the function:
curl -d '{"text": "hello"}' http://172.16.52.11:31112/function/add
exit status 1
Traceback (most recent call last):
File "index.py", line 15, in
ret = handler.handle(st)
File "/home/app/function/handler.py", line 11, in handle
item = request['data']['text']
TypeError: string indices must be integers, not str
OK this appears to be the reason. You can't deploy a code sample designed for one project straight into another, but it looks like a simple program, so with some adaptations you could do.
What do you want to achieve here? Just to access redis or to specifically port their sample to OpenFaaS for community-use?
I'd suggest you also create a GitHub repo and upload what you have now so we can advise you.
Alex
Hello @alexellis thank you for your answer,
That's what i tried to do i tried to adapted the way how is the python functions are written and executed in Openfaas.
but had those errors :(
Hello again @alexellis i created the Github repo with the changes that i did in the previous guestbook code.
Swarm or Kubernetes?
You have the errors because you're not following the OpenFaaS approach, you have it all set out wrong.
It's best for you to stop now and go over to the workshop and learn how to set out a function, then copy in the code you want after that. You can't copy/paste a code sample from another unrelated project and expect it to work.
https://github.com/openfaas/workshop
Alex
I'm using it in kubernetes .
Start by creating a new template:
faas-cli new --lang python3 openfaas-guestbook
mv openfaas-guestbook.yml stack.yml
Now edit ./openfaas-guestbook/handler.py and insert your code. You will want to parse the "req" object using payload = json.loads(req).
Edit ./openfaas-guestbook/requirements.txt and add your pip module for redis.
Now start adding code inside the handler to define the behaviours you want.
Derek close: inactivity
Most helpful comment
Hello @white-cloud. Its really difficult to tell from the detail provided, but this does look alike a python error. Have you tried validating the code outside of openfaas?
Is your code available in a repo anywhere? Can you provide enough detail to enable someone else to reproduce, please?