cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jjurisch
Regular Visitor

Post File via HTTP

I am trying to identify a method to use flow to send the attachment of an email via HTTP Post to a custom API. This API accepts multipart form data with file attachments and works when testing via PostMan. However, when trying to craft together the Flow steps I am unable to emulate the request. 

 

Is there any way to do this type of request via Flow and a custom API?

 

My Setup

Step 1) When an email is flagged. 

Step 2) For each attachment:

   A) HTTP

            Method: POST

            URL: Custom API URL

            Header: { "Content-Type": "application/x-www-form-urlencoded" }

            Body: [attachments]

            Authentication: None

  

When the API recieves the request, it appears to be the encoded file but not in a multipart form request. I have full control over how the API handles the request data so could go about this a different way, but I'm completely stuck with figuring out how the Body field is supposed to behave with an attachment. 

1 ACCEPTED SOLUTION

Accepted Solutions
jjurisch
Regular Visitor

I was able to finally self solve this with some more advanced trial and error. Hopefully it will help somebody else down the line until there is better documentation around the actions. 

 

It turns out that if you add the [attachments] object to the body of the email it will submit a JSON array with attributes of the file and the file itself encoded in Base64. So I just needed to update my API handler to accept a JSON array, loop through each attachment and decode the ContentBytes into a file again. 

 

I didn't experiment with adding additional attachment information to the body, but my use didn't need anything else from the message. 

 

Example Attachment Record

[{
    "Id": "AQMkADgxZmMyZmVmLWZiYTEtNGI3ZC05NzZlLTUzYTM1MGQ0YjdmNgBGAAAD5GChXHSojUuPkBPyKc4QiwcAZAodtTo4mUerxYfYMVtIcwAAAgEMAAAAZAodtTo4mUerxYfYMVtIcwAB6K4I0AAAAAESABAAszVi_Bwy20yVhdzX_og1fA==",
    "ContentType": "application/zip",
    "Size": 12552,
    "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
    "Name": "attachment.zip",
    "ContentBytes": "UEsDBAoAAAAIAK1RT0odhQmifC4AAGhABgAyAAAAZ29vZ2xlLmNvbSFlbmdhZ2UuM20uY29tITE0ODcwMzA0MDAhMTQ4NzExNjc5OS54bWztnduO20iWru/7KQp9n2FGkMETPJ49wMz0xQZmgJ4e7MsCRVISKylSICm7VE+/I6hDMm01My2RGYtRP2C5XHmQKOnTinX81+d//X1X/vI1b9qirv7lr5w5f/0lr9I6K6rNv/z1f//xn0/hX3/51y9/+bzO82yVpM9f/vLLL5+bfF833a+7vEuypEv019RX62bza5Xs8i+but6UOUvr3edP1y+efibfJUX5parVPZTHp2yXNOlTe9jru/s/w187/dz5d37vmuTXtK66JO1+Lap1/WXbdfs2/vTp/Kvs5Vc/JZ+Sqv2WN5+E5/sydNR9/fj7pzs+P40i++IKTwoZisAJXBmJIIzU7718+/Tj6qnmvzZJtTk/GfWlVb4pqi/cCwPHdTxH/dLpK5fv51XWf5dzP4gidSnV5c4+vb6366MNX9PP+7os0uOv+8OqLNptfr2QWr061Ze82iSbnLm700t2/urpR5Lsudh9aT5/Ov3j/MV2v+6/pv97+tJevRdV/vnT/vz/7eUL7eUr+7T7wvUz0//or/TWVanXMq2bywU29bfrS9DWhybNfy32X4Tj8FjKMF7n3I95FEfqb+7FXKqbH6rHvP7o5ZfT+lCph//86fSPy5fPV5B/TcqDehmzyzf0a1O0+7otOoXz+ZkMvzL4Of2y7JO2VT9wfYXOL8H6y1rRp1+D9fUhP91+TPW+XZ7r5yLLq65YF+rDdP21bZ5kefPruql3379fw2+d7+uHe/icHLrtr03eHsru5U6/u+C3cTjTru/l/JzP/zN41nmZp13dfMmeuSO8J+FwoV6By1evL8PwsT8PXqCfvo62Xnenl/n1tQxedkXqd09f//CFs3ch5ztBvA5XTqw+nCJOHRnHQiRAzQ7UNvqU+PlrmBYxJ2Scu0xZN8ZF8M/RcoHWgtCiZMUCj2
},]

View solution in original post

13 REPLIES 13
jjurisch
Regular Visitor

I was able to finally self solve this with some more advanced trial and error. Hopefully it will help somebody else down the line until there is better documentation around the actions. 

 

It turns out that if you add the [attachments] object to the body of the email it will submit a JSON array with attributes of the file and the file itself encoded in Base64. So I just needed to update my API handler to accept a JSON array, loop through each attachment and decode the ContentBytes into a file again. 

 

I didn't experiment with adding additional attachment information to the body, but my use didn't need anything else from the message. 

 

Example Attachment Record

[{
    "Id": "AQMkADgxZmMyZmVmLWZiYTEtNGI3ZC05NzZlLTUzYTM1MGQ0YjdmNgBGAAAD5GChXHSojUuPkBPyKc4QiwcAZAodtTo4mUerxYfYMVtIcwAAAgEMAAAAZAodtTo4mUerxYfYMVtIcwAB6K4I0AAAAAESABAAszVi_Bwy20yVhdzX_og1fA==",
    "ContentType": "application/zip",
    "Size": 12552,
    "@odata.type": "#Microsoft.OutlookServices.FileAttachment",
    "Name": "attachment.zip",
    "ContentBytes": "UEsDBAoAAAAIAK1RT0odhQmifC4AAGhABgAyAAAAZ29vZ2xlLmNvbSFlbmdhZ2UuM20uY29tITE0ODcwMzA0MDAhMTQ4NzExNjc5OS54bWztnduO20iWru/7KQp9n2FGkMETPJ49wMz0xQZmgJ4e7MsCRVISKylSICm7VE+/I6hDMm01My2RGYtRP2C5XHmQKOnTinX81+d//X1X/vI1b9qirv7lr5w5f/0lr9I6K6rNv/z1f//xn0/hX3/51y9/+bzO82yVpM9f/vLLL5+bfF833a+7vEuypEv019RX62bza5Xs8i+but6UOUvr3edP1y+efibfJUX5parVPZTHp2yXNOlTe9jru/s/w187/dz5d37vmuTXtK66JO1+Lap1/WXbdfs2/vTp/Kvs5Vc/JZ+Sqv2WN5+E5/sydNR9/fj7pzs+P40i++IKTwoZisAJXBmJIIzU7718+/Tj6qnmvzZJtTk/GfWlVb4pqi/cCwPHdTxH/dLpK5fv51XWf5dzP4gidSnV5c4+vb6366MNX9PP+7os0uOv+8OqLNptfr2QWr061Ze82iSbnLm700t2/urpR5Lsudh9aT5/Ov3j/MV2v+6/pv97+tJevRdV/vnT/vz/7eUL7eUr+7T7wvUz0//or/TWVanXMq2bywU29bfrS9DWhybNfy32X4Tj8FjKMF7n3I95FEfqb+7FXKqbH6rHvP7o5ZfT+lCph//86fSPy5fPV5B/TcqDehmzyzf0a1O0+7otOoXz+ZkMvzL4Of2y7JO2VT9wfYXOL8H6y1rRp1+D9fUhP91+TPW+XZ7r5yLLq65YF+rDdP21bZ5kefPruql3379fw2+d7+uHe/icHLrtr03eHsru5U6/u+C3cTjTru/l/JzP/zN41nmZp13dfMmeuSO8J+FwoV6By1evL8PwsT8PXqCfvo62Xnenl/n1tQxedkXqd09f//CFs3ch5ztBvA5XTqw+nCJOHRnHQiRAzQ7UNvqU+PlrmBYxJ2Scu0xZN8ZF8M/RcoHWgtCiZMUCj2
},]

Hi @jjurisch,

 

Thanks for the update and sharing!

Regards

Community Support Team _ Michael Shao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @jjurisch

I'm integrating a personnal app with Microsoft Flow and I'm Having the same problem.

Can you describe this steps:

 

"So I just needed to update my API handler to accept a JSON array, loop through each attachment and decode the ContentBytes into a file again." 

 

Thank you,

Filipe Amaro

 

SH2021
Frequent Visitor

Dear Community

 

I am facing this problem and I could not find a proper explanation. If anyone knows how to attach documents that are saved in SharePoint to an HTTP POST request used to create draft emails in Outlook, then please share this valuable piece of info!

 

I want to obtain "test.pptx" which lies in a SharePoint document folder and then attach it to my HTTP POST request. I first import all the data from this folder and then loop through the data in it. When the loop is at the test.pptx file, I obtain the "id" by using the dynamic content "identifier", as well as the "Name" by using "File Name with Extension". Interestingly, "File Name with Extension" gave me the contentType when saving it in an array. 
I furthermore load the data directly by using the identifier, which gives me the "ContentBytes" and I then get "Size" by using int(length(string(body('Dateiinhalt_abrufen')))) (its in German).

 

Below is one of many trials. I'd appreciate any feedback! For example, is the positioning of the attachment correct, or should it appear somewhere else?


Kind regards
Sean

 

{

  "subject": "attachment try ",
  "body":{
  "contentType":"HTML",
  "content":"Dear X, how are you doing? Bye, Y",},
  "toRecipients":[
  {
  "emailAddress":{
  "address":"me.me@me.com"}
  }
],
  "ccRecipients":[

  {
  "emailAddress":{
  "address":"me.me@me.com"}
  }
],
  [{
  "@odata.type": #Microsoft.OutlookServices.FileAttachment,
  "Id": "Shared%2bDocuments%252faktuelles%2bQuartal%252ftest_GH_.pptx" ,
  "Name": "test.pptx" ,
   "ContentType": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  "Size": 32731,
  "ContentBytes": "UEsDBBQA…”
  }]

}

 

Error upon running:

Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.

jjurisch
Regular Visitor

@SH2021 - most likely our use cases may be different as with mine I was controlling both the Flow application as well as the API it was sending the data to. In this case, I couldn't change anything in the Flow submission (that I could find). However, that was also ~4-5 years ago and I haven't kept up with advancements in PowerApps/Flows.

 

In my example, the payload that was sent from Flow to the downstream API was out of my control, but in examining what it was sending I found that the JSON line of "ContentBytes" was actually a Base64 encoded string. If you have control in the downstream application to examine that line and Base64 decode it, it will convert into a file if you use the other metadata to encode it correctly (Content Type, Name, etc). 

 

So if you are sending to Outlook as draft - unless you have the ability to parse/decode the string value into email values this may not help you much. Outside of this specific project I haven't dabbled with Flow/PowerApps since so am not much of a resource in this space. 

 

Good luck figuring it out! If you do, please add some details to this thread as looks like more people may have similar issues/use cases. 

SH2021
Frequent Visitor

Thank you for your reply @jjurisch! Unfortunately, I have not solved it, yet, but I might have gotten slightly closer. I used the code below. I got the code for "Attachments" by examining the code of another simple flow which saved this attachment into my sharepoint documents folder, so I can be sure that the information under "Attachments" is correct.

 

My problematic flow, which I hope will attach this saved file to a draft outlook e-mail using an HTTP POST Request, now gives me a different warning than before: "were unable to deserialize".

 

Can anyone tell me whether this relates to jjurisch comment about decoding the Base64 string? If yes, I suppose I should parse/decode the string value (meaning the strings of "Id" and "ContentType"? or all fields?) into email values?


Kind regards

{
"subject": "attachment ",
"body":{
"contentType":"HTML",
"content":"Dear X, how are you doing? Bye, Y",
"Attachments": [
{
"@odata.type": "#Microsoft.OutlookServices.FileAttachment",
"Id": "AAMkADkwOWU0MG…
"LastModifiedDateTime": "2022-01-27T15:06:43+00:00",
"Name": "test_BB_.xlsx",
"ContentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"Size": 18246,
"IsInline": false,
"ContentId": null,
"ContentBytes": "UEsDBBQABgAIAAAAIQDuoo…
}
]
},
"toRecipients":[
{
"emailAddress":{
"address":"me.me@me.com"}
}
],
"ccRecipients":[
{
"emailAddress":{
"address":"me.me@me.com"}
}
],

}

 

 

I alternatively tried to directly attach the email attachments to the HTTP POST Request (I have to use PA in German). No luck though:

SH2021_0-1643298984147.pngSH2021_1-1643299010029.png

 

The error message body upon examining:

{
  "error": {
    "code""BadRequest",
    "message""Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.",
    "innerError": {
      "date""2022-01-27T15:53:51",
      "request-id""ab80e8ef-e4ee-4927-a837-677f510b545c",
      "client-request-id""ab80e8ef-e4ee-4927-a837-677f510b545c"
    }
  }
}

 

 

tolga-balci
Frequent Visitor

@SH2021 - I'm struggling with the same for weeks. Were you able to solve your issue? 

SH2021
Frequent Visitor

Hi@tolga-balci I have not implemented a solution yet, but may have found one. Lately I stumbled upon something which I wanted to try out but then forgot to save the link. It may have been this, because I have not tried this compose idea yet and it looks like it may work: https://www.damobird365.com/create-a-draft-email-in-outlook/

I will try it out when it becomes relevant for me again (in one month probably), but please let me know if you beat me to it.

Kind regards

SH

tolga-balci
Frequent Visitor

Deal. To be honest, I'm following his article, too. I managed to get the drafts working without any issues, but things fail when I try to add the attachments. 

SH2021
Frequent Visitor

I looked at your post now (30.05.22). So you get the same problem is I am facing it seems, the payload error.

One thing I wonder about is that you have empty spaces in your "name" and "contentBytes":

“name”:”Monthly Report May 2022.xlsx”,
“contentBytes”: outputs(‘Get XLSX Report’)?[‘body’]?[‘$content’]


So far Power Automate has always replaced spaces with lower bars "_" whenever I reference previous steps. It does not seem to like empty spaces. Maybe manually replace them accordingly and try again?

 

Also: did you try Aud's solution?

Thanks @SH2021  - it seems that I corrected it afterwards, as it seems like there are the underscores, and the error message changed to:

 

{
  "error": {
    "code": "UnableToDeserializePostBody",
    "message": "were unable to deserialize "
  }
}

 

 

Plus, I think we are missing the base64 conversion as documented in the fileAttachments documentation.

 

Edit: Regarding my post on 30.May, it doesn't appear under my profile. It's as if I posted it and is somehow disappeared. 

That is a shame, but can you please send me the post's link?

SH2021
Frequent Visitor

tbalci
Frequent Visitor

@SH2021 

Sorry, I really got confused with another post.

 

Helpful resources

Announcements
October Events

Mark Your Calendars

So many events happening this month - don't miss out!

 WHAT’S NEXT AT MICROSOFT IGNITE 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Register for a Free Workshop.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

Users online (2,663)