I try to develop HTTP trigger azure function using C# and .net 3.0.1 (runtime ~3) and Visual Studio 2019. In my function I want to write data into blob and I want to be able set destination file name from request body. I use the following code:
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[Blob("reports/{reportname}", FileAccess.Write)] TextWriter report,
ILogger log)
{
But when I run function, I got error:
Unable to resolve binding parameter 'reportname'. Binding expressions must map to either a value provided by the trigger or a property of the value the trigger is bound to, or must be a system binding expression (e.g. sys.randguid, sys.utcnow, etc.).
I already read about bindings but I completely don't understand how to create correct binding or how to add binding into function.json file.
Can anyone please help/explain what do I do wrong?
Thanks!
CodePudding user response:
Firstly, you need to provide a value for {reportname} as the error message says.
As you are using a [HttpTrigger], you can provide the value in a POST request body:
{
"reportName": "test"
}
In the [HttpTrigger] binding, instead of getting a HttpRequest object, you can get the binding to pass you a deserialized object to represent your POST request i.e. MyRequest:
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] MyRequest request
When you send a request, the ReportName property will then get mapped in the [Blob] binding to the path: reports/{reportname}. This is the path in blob storage where your file will be stored.
You can then use the output TextWriter report parameter to add text to your blob:
report.WriteLineAsync("content");
Or you may wish to also send the report content in the POST body:
public class MyRequest
{
public string ReportName { get; set; }
public string ReportContent { get; set; }
}
report.WriteLineAsync(request.ReportContent);
Full working example:
public static class HttpTriggeredFunction
{
[FunctionName("HttpTriggeredFunction")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "reports")] MyRequest request,
[Blob("reports/{reportname}", FileAccess.Write)] TextWriter report,
ILogger log)
{
await report.WriteLineAsync(request.ReportContent);
return new OkResult();
}
}
public class MyRequest
{
public string ReportName { get; set; }
public string ReportContent { get; set; }
}


