Reputation: 5218
I'm having a hard time getting an image uploaded to Azure Functions using HttpRequest
. I'm specifically using HttpRequest
because that's what the VS templates use. I've simplified the problem to a simple test case with Postman.
Function:
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequest req)
{
return new CreatedResult("", req.Form.Files[0].OpenReadStream());
}
Postman:
Method: POST
URL: [Function URL]
Headers:
Content-Type: multipart/form-data
Body: binary with image selected
So what should happen is that I select a file to upload, I click "Send" to upload it to the Azure Function, which should automatically return the stream and display the picture in the Postman response.
But when I call it from Postman, an exception is thrown when it tries to read the stream:
"System.IO.InvalidDataException: Missing content-type boundary.\r\n at Microsoft.AspNetCore.Http.Features.FormFeature.GetBoundary(MediaTypeHeaderValue contentType, Int32 lengthLimit)\r\n at Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken)\r\n at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()\r\n at Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest.get_Form()\r\n at [My code]"
Now if I were to switch this to use HttpRequestMessage
like the old Azure Function templates and use req.Content.ReadAsStreamAsync()
, it works like a charm.
But since the new templates use HttpRequest
, this question is focused on the use of HttpRequest
and its correct use. So how can I use HttpRequest
to get this to work?
Upvotes: 1
Views: 2072
Reputation: 17790
Postman provides a way to upload form-data, choose type as File and upload the image.(No need to set headers)
See this request header in Fiddler, Postman sets boundary automatically.
content-type: multipart/form-data; boundary=--------------------------970956809380964389175281
If we upload binary with content type header, the header is Content-Type: multipart/form-data
, so that we got error of missing boundary.
Upvotes: 2