This answer explains how one can send multipart form data via HTTP POST in C#:
https://stackoverflow.com/a/19664927/10779
Code quoted from answer:
private async Task<System.IO.Stream> Upload(string actionUrl, string paramString, Stream paramFileStream, byte [] paramFileBytes)
{
HttpContent stringContent = new StringContent(paramString);
HttpContent fileStreamContent = new StreamContent(paramFileStream);
HttpContent bytesContent = new ByteArrayContent(paramFileBytes);
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
formData.Add(stringContent, "param1", "param1");
formData.Add(fileStreamContent, "file1", "file1");
formData.Add(bytesContent, "file2", "file2");
var response = await client.PostAsync(actionUrl, formData);
if (!response.IsSuccessStatusCode)
{
return null;
}
return await response.Content.ReadAsStreamAsync();
}
}
How would you properly receive such a POST request in an Azure function?
It seems the HttpRequest
Form property (req.Form["file1"]
) always gives you a string instead of access to a stream. How do you correctly get the original byte[] data for the "file1" or "file2" key?