Tweeting Using Logic Apps: Error “media type unrecognized” when sending image with Tweets

Time to Read: 3 minutes

BackGround

Today while going through the Logic Apps forums , I found a question where the Original Poster had problems sending an image with the tweet text by using the twitter api. The error that the Logic App run time threw back was similar to

{
  "status": 400,
  "message": "media type unrecognized.\r\nclientRequestId: 5c3cf7d0-c018-494d-bc74-f4d88edc040d\r\nserviceRequestId: 392220ba2115f611f7789f451003bdc4",
  "source": "twitter-wi.azconn-wi.p.azurewebsites.net"
}

in this post I have tried to dissect and solve the issue.

Constructing The Logic App1

The logic app is simple. It accepts the tweet text and the image text in a base64 string. I have used the HTTP request trigger to fire up the Logic App. The message sent to the logic app looks something like following

{
    "tweetText" : "Sample Tweet With Image Fired From Logic App",
    "imageData" : "base64stringHere"
}

I then added the Twitter “Post a tweet ” action to my logic app and passed the data from the incoming message directly to the action.

My logic app looks like below.

LogicAPpDesign1.JPG

Testing the Logic App1

I tested the logic app using POSTMAN and I got following error message, when I examined the LogicApp Overview.

Test1.JPG

Same “media Type is unrecognized” error as posted in the forums. So I was able to replicate the issue.

Constructing the Logic App2

Let us look at the documentation of the twitter connector on the connectors page at Twitter Connector it says that to post a tweet it has two paramters

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

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

LogicAPp2.JPG So my modified Logic App looks like

LogicApp21.JPG

The code behind the Twitter connector is as follows.

{
    "inputs": {
        "host": {
            "connection": {
                "name": "@parameters('$connections')['twitter']['connectionId']"
            }
        },
        "method": "post",
        "body": "@base64ToBinary(triggerBody()?['imageData'])",
        "path": "/posttweet",
        "queries": {
            "tweetText": "@triggerBody()?['tweetText']"
        },
        "authentication": "@parameters('$authentication')"
    }
}

So the base64ToBinary Converts the base64 to bainary data

Testing Logic App 2

I tested the modified logic app with the same data and it ran successfully.

SuccessfulTweet.JPG

A look at twitter page

SuccessfulTweet2.JPG

Conclusion

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

 

Programmer by profession, curious by nature.