This repository was archived by the owner on Jun 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
HttpFormAttribute
Jussi Saarivirta edited this page Jan 26, 2019
·
1 revision
Annotation for a HttpParam indicating that its value comes from the HttpRequest form parameters.
Contains the Name (indicates the form parameter name) and Required (if true, throws exception when parameter not provided) properties.
Content types of both application/x-www-form-urlencoded and multipart/form-data (including file uploads) are supported.
Default deserialization can convert form parameter values into objects, assuming that the provided value is valid JSON.
POST http://localhost:7071/api/form-basics
Content-Type: application/x-www-form-urlencoded
someString=hello&someObject=%7B%22Name%22%3A%22John%22%2C%22Bool%22%3A%22true%22%7D&someInteger=123&stringList=5&stringList=6&stringList=7&enumArray=0&enumArray=1
[FunctionName("FormParametersDemo1")]
public static async Task<IActionResult> FormParametersDemo1(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "form-basics")] HttpRequest req,
[HttpForm]HttpParam<string> someString,
[HttpForm]HttpParam<SomeClass> someObject,
[HttpForm(Required = true, Name = "someInteger")]HttpParam<int> integer,
[HttpForm]HttpParam<List<string>> stringList,
[HttpForm]HttpParam<SomeEnum[]> enumArray,
ILogger log)
{
log.LogInformation($"someString: {someString}");
log.LogInformation($"someObject: {JsonConvert.SerializeObject(someObject.Value)}");
log.LogInformation($"integer: {integer}");
log.LogInformation($"stringList: {JsonConvert.SerializeObject(stringList.Value)}");
log.LogInformation($"enumArray: {JsonConvert.SerializeObject(enumArray.Value)}");
return new OkObjectResult("ok");
}POST http://localhost:7071/api/form-upload
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="someString"
hello
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="image"; filename="test.jpg"
Content-Type: image/jpeg
MIME-Version: 1.0
< c:/temp/temp.jpg
------WebKitFormBoundary7MA4YWxkTrZu0gW--
[FunctionName("FormParametersDemo2")]
public static async Task<IActionResult> FormParametersDemo2(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "form-upload")] HttpRequest req,
[HttpForm]HttpParam<string> someString,
[HttpForm(Name = "image")]HttpParam<IFormFile> file,
ILogger log)
{
log.LogInformation($"someString: {someString}");
log.LogInformation($"File information: name: {file.Value?.Name}, fileName: {file.Value?.FileName}, size: {file.Value?.Length}");
return new OkObjectResult("ok");
}Note: This is not really different from accessing req.Form.Files, except that it's in the Function signature, allowing some code analysis with reflection.
POST http://localhost:7071/api/form-upload-multi
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="someString"
hello
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="image1"; filename="test.jpg"
Content-Type: image/jpeg
MIME-Version: 1.0
< c:/temp/temp.jpg
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="image2"; filename="test.jpg"
Content-Type: image/jpeg
MIME-Version: 1.0
< c:/temp/temp.jpg
------WebKitFormBoundary7MA4YWxkTrZu0gW--
[FunctionName("FormParametersDemo3")]
public static async Task<IActionResult> FormParametersDemo3(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "form-upload-multi")] HttpRequest req,
[HttpForm]HttpParam<string> someString,
[HttpForm]HttpParam<IFormFileCollection> files,
ILogger log)
{
log.LogInformation($"someString: {someString}");
foreach (var file in files.Value)
{
log.LogInformation($"File information: name: {file.Name}, fileName: {file.FileName}, size: {file.Length}");
}
return new OkObjectResult("ok");
}Table of contents
- Overview
- Authentication and authorization
- Exception handling
- HttpParam Attributes
-
Classes and interfaces overview
- Implementable interfaces
- Infrastructure classes