Azure-functions-host: Advice on returning Content from HTTPTrigger Azure Functions function

Created on 2 Sep 2016  路  11Comments  路  Source: Azure/azure-functions-host

Hi @dsyme,

I've used your example https://github.com/Azure/azure-webjobs-sdk-script/blob/dev/sample/HttpTrigger-FSharp/run.fsx for a HttpTrigger AF. I do get a 200 but unfortunately no content. I've triggered it from postman. I see that Content isn't a valid HttpResponseMessage constructor parameter so I supply it, as a test for now, just before the return:

let res =
            match queryParams.TryGetValue("name") with 
            | true, name -> 
                new HttpResponseMessage(HttpStatusCode.OK, Content = new StringContent("Hello " + name))
            | _ -> 
                new HttpResponseMessage(HttpStatusCode.BadRequest, Content = new StringContent("Please pass a name on the query string"))

        res.Content <- new StringContent("Hello ")
        log.Info(sprintf "Response=%A" res)        
        return res

Do you have any advice on getting this to return content?

Warmest regards,

Garrard.

Most helpful comment

Hi @ahmelsayed, oh dear, drops head in shame, I didn't have an output http bindng. Working as expected.

All 11 comments

Are you sure your function is compiling? that Content isn't in the constructor, that's F#'s object initializer syntax.

Here is a simple HttpTrigger F# function

open System
open System.Linq
open System.Net
open System.Net.Http
open System.Threading.Tasks
open Microsoft.Azure.WebJobs.Host

let Run (req: HttpRequestMessage) =
    let res = HttpStatusCode.OK |> HttpResponseMessage
    res.Content <- new StringContent("test")
    res

Hi @ahmelsayed,

I only included a snippet of @dsyme's example in my post above and yes it did compile. Here it is in it's entirety

#if !COMPILED
#I @"../../bin/Binaries/WebJobs.Script.Host"
#r "Microsoft.Azure.WebJobs.Host.dll"
#r "System.Web.Http.dll"
#r "System.Net.Http.dll"
#endif

open System
open System.Linq
open System.Net
open System.Net.Http
open System.Threading.Tasks
open Microsoft.Azure.WebJobs.Host

let Run (req: HttpRequestMessage , log: TraceWriter) : Task<HttpResponseMessage> =
    async { 
        let queryParams = req.GetQueryNameValuePairs().ToDictionary((fun p -> p.Key), (fun p -> p.Value), StringComparer.OrdinalIgnoreCase)

        log.Info(sprintf "F# HTTP trigger function processed a request. Name=%A" req.RequestUri)

        log.Info(sprintf "param=%A" queryParams)

        let res =
            match queryParams.TryGetValue("name") with 
            | true, name -> 
                new HttpResponseMessage(HttpStatusCode.OK, Content = new StringContent("Hello " + name))
            | _ -> 
                new HttpResponseMessage(HttpStatusCode.BadRequest, Content = new StringContent("Please pass a name on the query string"))

        res.Content <- new StringContent("Hello ")
        log.Info(sprintf "Response=%A" res)

        return res
    } |> Async.StartAsTask


I added the...

        res.Content <- new StringContent("Hello ")
        log.Info(sprintf "Response=%A" res)

...at the bottom to see if I could get any content to return to client.

I will use your example. Thanks!

@dsyme I'm seeing users copying these#I @"../../bin/Binaries/WebJobs.Script.Host" statements into their functions. That's not correct, is it? Should the samples in our repo even have those lines (all the #if !COMPILED stuff)?

@garrardkitchen, your sample code works as expected for me.

@mathewc I removed those from the templates, but they shouldn't have any impact when running in Azure Functions (since there's a condition looking at the COMPILED debug symbol). They're there to make it easier to edit those files in VS/IDEs that support F#.

Hi @ahmelsayed, do you see 'Hello' in the body response? I don't. What client are you using?

Hi @fabiocav, this is exactly why I've left them in there for now.

Hi @ahmelsayed, oh dear, drops head in shame, I didn't have an output http bindng. Working as expected.

We might be able to improve usability by detecting things like this - returning a response when there isn't an output binding to receive it. Similarly for other languages - if we see you writing some output bindings that go nowhere, we could log a warning.

That's a really good idea @mathewc...I'm sure I won't be the only one to fall at that particular hurdle! :o)

Was this page helpful?
0 / 5 - 0 ratings