Giter VIP home page Giter VIP logo

flask-openwhisk's People

Contributors

alexmilowski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

flask-openwhisk's Issues

Content-Type application/json fails with OpenWhisk

Steps to reproduce

  1. Pack the following files from this repo into a zip file: web.py flaskwsk and __main__.py
  2. Deploy in Open Whisk
  3. Send a POST request with a JSON payload, such as:
curl --request POST \
  --url https://eu-gb.functions.cloud.ibm.com/api/v1/web/salvador.de.la.puente.gonzalez%40ibm.com_dev/default/flask_ow_test/echo \
  --header 'content-type: application/json' \
  --cookie __cfduid=dee158ca1a645cf64da8b23de7b44a18b1586521938 \
  --data '{
	"hello": "world"
}'

Current behaviour

The following exception happens in exec__.py:

Activation: 'flask_ow_test' (8ed365ca91f245939365ca91f25593a1)
[
    "2020-04-11T15:19:13.981350Z    stderr: Traceback (most recent call last):",
    "2020-04-11T15:19:13.981400Z    stderr: File \"/action/1/src/exec__.py\", line 70, in <module>",
    "2020-04-11T15:19:13.981406Z    stderr: out.write(json.dumps(res, ensure_ascii=False).encode('utf-8'))",
    "2020-04-11T15:19:13.981410Z    stderr: File \"/usr/local/lib/python3.7/json/__init__.py\", line 238, in dumps",
    "2020-04-11T15:19:13.981414Z    stderr: **kw).encode(obj)",
    "2020-04-11T15:19:13.981418Z    stderr: File \"/usr/local/lib/python3.7/json/encoder.py\", line 199, in encode",
    "2020-04-11T15:19:13.981421Z    stderr: chunks = self.iterencode(o, _one_shot=True)",
    "2020-04-11T15:19:13.981425Z    stderr: File \"/usr/local/lib/python3.7/json/encoder.py\", line 257, in iterencode",
    "2020-04-11T15:19:13.981429Z    stderr: return _iterencode(o, 0)",
    "2020-04-11T15:19:13.981433Z    stderr: File \"/usr/local/lib/python3.7/json/encoder.py\", line 179, in default",
    "2020-04-11T15:19:13.981438Z    stderr: raise TypeError(f'Object of type {o.__class__.__name__} '",
    "2020-04-11T15:19:13.981442Z    stderr: TypeError: Object of type bytes is not JSON serializable",
    "2020-04-11T15:20:13.815Z       stderr: The action did not initialize or run as expected. Log data might be missing."
]

Expected behaviour

JSON is echoed.

Cause

The OpenWhisk launcher for Python 3 uses json.dumps() to write the request into out but the flaskwsk adaptor postprocess the body of the Flask response before sending it to Open Whisk. Specifically, it encodes the body into base64 if it is not a text content type. That causes the exception.

I solved it by reverting the base64 encoding and actually decoding the bytes into utf-8 but I'd be happy to discuss a better solution and provide a PR.

from base64 import b64decode

from flaskwsk import invoke

from web import app

def main(args: dict) -> dict:
    ow_response = invoke(app, args)
    content_type = ow_response.get('headers', {}).get('Content-Type', 'application/octet-stream')
    if content_type == 'application/json':
        data = b64decode(ow_response['body']).decode('utf-8')
        ow_response['body'] = data

    return ow_response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.