Asked by:
How to handle a POSTed file to an ASP.NET Core application from Microsoft Flow

Question
-
I have a Microsoft Flow setup with the following steps
- When a file is created (SharePoint) (this is pointing to a specific document library
- Http (Http), Method: Post, Uri (pointing to my app), Body: File Content from the SharePoint step above.
My app that I have written is hosted in an Azure Web App and is written in C# in ASP.NET Core. I'm trying to convert the information that is coming into me from Flow back into the original document and I cannot figure out what is being sent. When I look in Flow it says the body of the request that was sent is:
{ "$content-type": "image/jpeg", "$content": "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAQABADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD1C9EMuqzGK1juS+3P7rccgc4yMYxjv1q/ol0I4bfTpQVniXaoyDuQHjoTg7ccGsDU7O+0+xEdoJfMUKiKE84MB/dJ5B9mzj6VneFtO1271qx1G+hubaGBjmCSUfMSMZZQNoxzgDnPfGKqcnypEJW1R//Z" }
In my application I originally tried to have a method that looks like
[HttpPost] public IActionResult Process(List<IFormFile> files)
but that flow is not submitted a file, so I changed it to the below method
[HttpPost] public IActionResult ProcessJson([FromBody]FlowFile file) {
...}
public class FlowFile { [JsonProperty(PropertyName = "$content-type")] public string ContentType { get; set; } [JsonProperty(PropertyName = "$content")] public string Content { get; set; } }
I also added a block of middleware, so that I could get the actual request body stream. The middleware that I added has method that looks like this
public async Task Invoke(HttpContext context) { var requestBodyStream = new MemoryStream(); var originalRequestBody = context.Request.Body; await context.Request.Body.CopyToAsync(requestBodyStream); requestBodyStream.Seek(0, SeekOrigin.Begin); var requestBodyText = new StreamReader(requestBodyStream).ReadToEnd(); var bytes = StringToByteArray(requestBodyText); LogRequestMiddleware.LastBody = System.Text.Encoding.Default.GetString(bytes); requestBodyStream.Seek(0, SeekOrigin.Begin); context.Request.Body = requestBodyStream; await next(context); context.Request.Body = originalRequestBody; } private static byte[] StringToByteArray(string hex) { return Enumerable.Range(0, hex.Length) //.Where(x => x % 2 == 0) .Select(x => Convert.ToByte(hex.Substring(x, 1), 8)) .ToArray(); }
When I do this The response that I get is below and I cannot figure how to get it back to the correct string that I want.

�����JFIF��`�`�����C�		

 $.' ",#(7),01444'9=82<.342���C			

2!!22222222222222222222222222222222222222222222222222�����"��������������	
�������}�!1AQa"q2���#B��R��$3br�	
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�����������������������������������������������������������������������������������	
������w�!1AQaq"2�B����	#3R�br�
$4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�����������������������������������������������������������������������������?������+X�K�����21�c�Z��]�ӥg�v��;�:����������P����I�f�>���m;]�֬u�nm���`�Q�1�P6�s�9�|b�r|���G�
- Moved by CoolDadTx Thursday, August 17, 2017 2:52 PM ASP.NET related
Thursday, August 17, 2017 12:46 PM
All replies
-
Thursday, August 17, 2017 1:25 PM