I have the following code in my Function App
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
var data = await req.Content.ReadAsAsync<PostData>();
var sid = data.sid;
log.Info($"sid ={sid}");
return req.CreateResponse(HttpStatusCode.OK, $"Current Time : {DateTime.Now}");
}
public class PostData
{
public string sid { get; set; }
}
The error message is
No MediaTypeFormatter is available to read an object of type 'PostData' from content with media type 'application/x-www-form-urlencoded'.
How do I set the function up to work with the correct media type?
[Update]
If I change the code to
var content = req.Content;
var jsonContent = await content.ReadAsStringAsync();
log.Info(jsonContent);
I can see the jsonContent text logged starting with
ToCountry=AU&ToState=&SmsMessageSid=SM8cac6c6a851 etc
But I am unclear on how to go about extracting the data I need.
I tried adding
dynamic results = JsonConvert.DeserializeObject<dynamic>(jsonContent);
and
using Newtonsoft.Json;
however this causes a script compilation error
[Update] Studying the sample code on the Integration Tab
Example C# code for a GitHub WebHook function
#r "Newtonsoft.Json"
using System;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
string jsonContent = await req.Content.ReadAsStringAsync();
log.Info("Hi 1"); // does log
dynamic data = JsonConvert.DeserializeObject(jsonContent);
log.Info("Hi 2"); // does not log
return req.CreateResponse(HttpStatusCode.OK, $"Current Time : {DateTime.Now}"
});
}
This produces an error
System.AggregateException : One or more errors occurred. ---> Unexpected character encountered while parsing value: T. Path '', line 0, position 0.
at Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.GetTaskResult(Task task)