2
votes

I have Visual Studio 2017 15.3 and have installed the Azure Development workload installed.

I created a blank HttpTrigger according to this article:

http://www.c-sharpcorner.com/article/getting-started-with-microsoft-azure-functions-using-visual-studio-2017/

I can debug successfully if I use Name as parameter query string.

However, If I use Postman to create this Post request:

{
    "name": "Azure"
}

I get the following error:

"mscorlib: Exception while executing function: HttpTrigger . Anonymously Hosted DynamicMethods Assembly: 'Newtonsoft.Json.Linq.JObject' doe s not contain a definition for 'name'."

Here is my code for the function app in Visual Studio 2017:

using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

namespace VS2017TestFunctionApp
{
    public static class HttpTrigger
    {
        [FunctionName("HttpTrigger")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            // parse query parameter
            string name = req.GetQueryNameValuePairs()
                .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                .Value;

            // Get request body
            dynamic data = await req.Content.ReadAsAsync<object>();

            // Set name to query string or body data
            name = name ?? data?.name;

            return name == null
                ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
                : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
        }
    }
}

Also, if I copy the exact same code within the Run function and use the same test post in a function app via the Azure Portal it all works fine.

enter image description here

1

1 Answers

0
votes

I create a Azure function app with your code and send a request using Postman, which works fine on my side.

Request and Response

enter image description here

Request body:

enter image description here

The breakpoint at getting request body can be hit as expected

enter image description here

edit:

I can extract name from data without any error.

enter image description here