openfaas / faas

OpenFaaS - Serverless Functions Made Simple
https://www.openfaas.com
Other
25.2k stars 1.94k forks source link

How do I get a JSON response from the Python template and view it in the OpenFaaS Legacy UI? #1762

Closed atlas-wheatear closed 2 years ago

atlas-wheatear commented 2 years ago

My actions before raising this issue

Why do you need this?

I really like the debugging UI, and would very much appreciate if I could view error JSON bodies without having to repeat the request in an external API debugger like cURL or Postman.

Expected Behaviour

The JSON body provided for the error is stringified and printed in the response-textarea.

Current Behaviour

The string [object Object] is printed beneath a newline. This is usually what you get when you coerce a JS object to a string.

Are you a GitHub Sponsor (Yes/No?)

Check at: https://github.com/sponsors/openfaas

List All Possible Solutions and Workarounds

Modify this line here to check the type of the data from the error, and JSONify if it's an object - it currently assumes its always a string: https://github.com/openfaas/faas/blob/52d0165c3f22b5d1c269dd6e43e6e6bda6559936/gateway/assets/script/bootstrap.js#L205

An example of how to do this exists in the code a few lines above: https://github.com/openfaas/faas/blob/52d0165c3f22b5d1c269dd6e43e6e6bda6559936/gateway/assets/script/bootstrap.js#L190

Which Solution Do You Recommend?

I only suggested one solution - this is the one I recommend.

Steps to Reproduce (for bugs)

  1. Get the code from https://github.com/emoulsdale/openfaas-test
  2. run faas-cli publish
  3. navigate to the gateway UI to which you just published
  4. invoke the function say-hello (with any body).

Context

## Your Environment

I'd like to end by saying that I'm intending to make a PR to change this later.

alexellis commented 2 years ago

Hey @emoulsdale thanks for your interest in OpenFaaS

Can you confirm what the Content-Type is being returned by your API?

Show us the output of:

curl -i http://127.0.0.1:8080/function/name

FYI, your example doesn't return JSON or a proper content-type header, so I'm not sure why you're expecting it to do so?

Have a look at "Custom Response Headers" => https://github.com/openfaas/python-flask-template#custom-response-headers

Alex

alexellis commented 2 years ago

/add label: support, question, python, templates

alexellis commented 2 years ago

Seems like your function's content type was not set to JSON :)

def handle(event, context):
    return {
        "statusCode": 200,
        "body": {"message": "Hello from OpenFaaS!"},
        "headers": {
            "Content-Type": "application/json"
        }
    }

Screenshot from 2022-10-03 15-19-44

I'll get this closed.

Bear in mind that it costs us money to answer every support question like this.

If you're using OpenFaaS, consider becoming a sponsor to OpenFaaS on a set monthly amount or some custom amount that you feel represents the value of the work we do better.

Alex

alexellis commented 2 years ago

/add label: invalid

alexellis commented 2 years ago

Btw, don't worry about the invalid label, it's just to help us track real issues and breakage vs user support.

You can meet us every week on Wednesdays https://docs.openfaas.com/community/

I also took the opportunity to improve the docs for the template that you're using to add the JSON example:

https://github.com/openfaas/python-flask-template