Tweeting Using Logic Apps - Error "Media Type Unrecognized" When Sending Image With Tweets

BackGround

 
This article examines the following error when tweeting images using Logic Apps and explains how to fix it.
  1. {  
  2.   "status": 400,  
  3.   "message""media type unrecognized.\r\nclientRequestId: 5c3cf7d0-c018-494d-bc74-f4d88edc040d\r\nserviceRequestId: 392220ba2115f611f7789f451003bdc4",  
  4.   "source""twitter-wi.azconn-wi.p.azurewebsites.net"  
  5. }  

Replicating The Error

 
The logic app to replicate this error is very simple. It looks like below.
 
 Tweeting Using Logic Apps: Error "media type unrecognized" when sending image with Tweets
 
The imageData property is used to pass the image as a base64string. The data is sent to the Logic App using an HTTP trigger and the  postTweet action posts the tweet to the defined Twitter connection.
 

Testing the Logic App

 
After testing the logic app through any REST Test tool, we get the following error in the Logic App run history.
 
Tweeting Using Logic Apps - Error "Media Type Unrecognized" When Sending Image With Tweets 
 
As the issue is now recognized, let us try to inspect and fix the issue.
 

Reconstructing the Logic App 

 

Let us look at the documentation of the Twitter Connector on the connectors page. At the Twitter Connector page, it says that to post a tweet, it has two parameters -

  1. tweetText which is a string
  2. Media which is binary

Once we look at this, the solution comes to us easily. The reason the logic app's action failed was that we were passing the base64 encoded string instead of a binary stream. So, we need to convert the base64 string into a binary and we should be good to go. So, I made the following changes to the logic app.

We need to parse the string in the imageData to binary using the base64ToBinary() function available in the logic app. The modified Logic App looks like the following.
 
Tweeting Using Logic Apps - Error "Media Type Unrecognized" When Sending Image With Tweets
 
Tweeting Using Logic Apps - Error "Media Type Unrecognized" When Sending Image With Tweets 
 
Let us peek at the code behind the PostTweet action. It is shown below.
  1. {  
  2.     "inputs": {  
  3.         "host": {  
  4.             "connection": {  
  5.                 "name""@parameters('$connections')['twitter']['connectionId']"  
  6.             }  
  7.         },  
  8.         "method""post",  
  9.         "body""@base64ToBinary(triggerBody()?['imageData'])",  
  10.         "path""/posttweet",  
  11.         "queries": {  
  12.             "tweetText""@triggerBody()?['tweetText']"  
  13.         },  
  14.         "authentication""@parameters('$authentication')"  
  15.     }  
  16. }  
We can see that the imageData is converted first into a binary stream and then sent to the Twitter API.
 

Retesting the Logic App

 
A successful run is shown below.
 
Tweeting Using Logic Apps - Error "Media Type Unrecognized" When Sending Image With Tweets 
 
Let us take a look at the Twitter handle to confirm if the image is posted correctly.
 
Tweeting Using Logic Apps - Error "Media Type Unrecognized" When Sending Image With Tweets

Conclusion

 
It is necessary to manage the content in the request and parse it into binary format while posting an image to Twitter using Logic App. 


Similar Articles