openfaas / of-watchdog

Reverse proxy for STDIO and HTTP microservices
MIT License
259 stars 115 forks source link

Support request about content length #104

Closed pyramation closed 3 years ago

pyramation commented 3 years ago

Expected Behaviour

when POSTing a request to a cloud funtion, the request body should be available inside the cloud function. Specifically, the header for content-length should be set and the data should be readable.

We should expect, for example,

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"a":"1","b":"2"}' \
http://localhost:31112/function/py2fn.openfaas-fn

to respond with:

{"function": "ok", "params": {"a": "1", "b": "2"} }

Current Behaviour

Currently the response is missing the body completely. When I check content length it is not > 0, hence the result:

{"function": "ok", "params": {} }

area of interest: https://github.com/pyramation/python-template-openfaas-of-watchdog-issue/blob/master/template/python3/index.py#L31-L34

Steps to Reproduce (for bugs)

  1. pull the template
faas-cli template pull https://github.com/pyramation/python-template-openfaas-of-watchdog-issue
  1. create a new functions
faas-cli new --lang python2.7 py2fn
faas-cli new --lang python3 py3fn
  1. build and deploy

Since the functions are just passing through params, we can build and deploy them immediately:

export OPENFAAS_URL=localhost:31112
faas build -f ./py2fn.yml
faas build -f ./py3fn.yml
faas deploy -f ./py2fn.yml
faas deploy -f ./py3fn.yml
  1. make requests and see both have same result
curl --header "Content-Type: application/json"   --request POST   --data '{"a":"1","b":"2"}' http://localhost:31112/function/py2fn.openfaas-fn
> {"function": "ok", "params": {} }

curl --header "Content-Type: application/json"   --request POST   --data '{"a":"1","b":"2"}' http://localhost:31112/function/py3fn.openfaas-fn
> {"function": "ok", "params": {} }
  1. verify that it works locally, with proper responses

For python 2.7

cd ./build/py2fn
touch function/__init__.py && PORT=10101 python index.py

# now on another shell...
curl --header "Content-Type: application/json"   --request POST   --data '{"a":"1","b":"2"}' http://localhost:10101
> {"function": "ok", "params": {"a": "1", "b": "2"}}

For python 3

cd ./build/py3fn
touch function/__init__.py && PORT=10101 python3 index.py

# now on another shell...
curl --header "Content-Type: application/json"   --request POST   --data '{"a":"1","b":"2"}' http://localhost:10101
> {"function": "ok", "params": {"a": "1", "b": "2"}}

Context

Trying to create languages for of-watchdog, so I can manipulate headers and create a connector that I'm working on in postgres. Currently nodejs works, so I'm a bit baffled by this body parsing issue.

Your Environment

alexellis commented 3 years ago

Can I ask why you need the content length and how you are using it? You're seeing -1 due to a chunked HTTP request, where the data is streamed so no length is available

alexellis commented 3 years ago

/set title: Support request about content length

pyramation commented 3 years ago

I'm using a built-in python3 library, http.server. All of my research leads to similar methods that read content-length header to parse the body of the request.

After reading your reply, I started to research into chunked encoding, but I'm not sure if http.server can do it. Did you mention there is a way to tell the of-watchdog system to use content-length?

pyramation commented 3 years ago

Thanks again! I think I'm just gonna use the flask template for now, seems light weight enough! Thanks for the info, I'm happy to understand the inter-workings and now accept that I should just use a system that abstracts the chucked/content-length