Templated http trigger function when adding new function fails when name passed as form content
VS 2017 preview 15.3 Azure functions ext 0.2
Not sure if this is correct repo to raise in...
When I add a new function http trigger the code that is scaffolded includes this
[FunctionName("HttpTriggerCSharp")]
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);
}
this works as expected when the name=foo is passed in the url
But I see an exception when the value is passed in the form body as implied by the code
No MediaTypeFormatter is available to read an object of type 'Object' from content with media type 'application/x-www-form-urlencoded'
This is caused by this line
dynamic data = await req.Content.ReadAsAsync<object>();
I could get it to work by changing the code to
// Get request body
var data = await req.Content.ReadAsFormDataAsync();
// Set name to query string or body data
name = name ?? data.GetValues("name").First();
Its made slightly worse as the error is talking about media type formatter which leads to a lot of issues that seem to be connected to the AzureWebJobsStorage setting
How are you calling the function? Try sending it as just raw JSON.
ahh got it yes that works - thanks
I wonder if it might be worth changing the help/error message returned - or comment in the code to make it clear thats what the ReadAsAsync
@richardjharding I don't think that the error message is unclear personally... the media type formatter is more around http content serialization/deserialization. I haven't dug into the code, but I believe that it is likely that the HttpTriggerAttribute binding is only reading in the format application/json (if that is the formatter or a more manual way to reading the content, I don't know - it may be because they are sharing trigger semantics across languages).
A .Net WebApi project for example has the formatters already enabled for a variety of content shapes (ie, application/x-www-form-urlencoding), hence how they work with a wider variety of http content from the initial template.
For reference, I haven't really dug into how to provide some other formatters, but it is likely to just be the case of creating a custom binding.
Just thought I'd add this incase others wondered about this reading your question :)
Thanks for following up.
Thank you for your solution @TheeJamesLee I'm facing the same situation and I cannot send raw JSON because it's someone else that is sending a regular POST to the function.