1

I'm trying to return a JSON object from Azure Function, and this examples, I mean, examples where response creation performing through context.res not working at all.

context.res = {
   body: {"name": "JSON STATHAM"}, //No. No mistake.
   contentType: 'application/json'
};

Why?

Only through context.done it's working if passed as a second parameter.

1 Answer 1

5

Look at how your http output binding name property is specified. At one time our templates/samples were defaulting to using $return for the output binding name. Using $return that means the response is expected to be the return value from the function:

{
    "bindings": [
        {
            "type": "httpTrigger",
            "name": "req",
            "direction": "in",
            "methods": [ "get" ]
        },
        {
            "type": "http",
            "name": "$return",
            "direction": "out"
        }
    ]
}

In that mode, only the value returned via context.done (i.e. the function return value) will be used. Change $return to some other name of your choosing and you can use context.res.

Sign up to request clarification or add additional context in comments.

1 Comment

I wish it wold be better documented. Thank you @mathewc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.