Direct_messages/events/new returns 415 if media not set

directmessages

#1

Hi,

Trying to use POST direct_messages/events/new to send direct message.
https://dev.twitter.com/rest/reference/post/direct_messages/events/new

It works if media id is attached, but if trying to send message without media it always fails with HTTP 415 (Unsupported Media Type). Media is defined as “optional” in documentation, so I wonder what could be wrong there.

Also documentation said that “text” should be url-encoded if necessary but this doesn’t seem to work either. Message appears non-decoded (e.g. “Hello%20World”) in Twitter messages.

Br,
Evgeny


#3

Hi @Evgeny18307110, can you share the JSON post body you are including in the request?


#4

Hi @joncipriano, I have actually found where the problem was. I had an extra “comma” in JSON body after message data object and it resulted in this strange error:

{ “event”: { “type”: “message_create”, “message_create”: {“target”: {“recipient_id”: “XXXXXX” }, “message_data”: {“text”: “test” }, } } }

Removing extra comma helped and now message is sent. But I still cannot get message text encoding working. If I send message url-encoded (as docs are saying "The text of your Direct Message. URL encode as necessary. ") receiver gets this message with percent encoding included. E.g. if I send:

{ “event”: { “type”: “message_create”, “message_create”: {“target”: {“recipient_id”: “XXXXX” }, “message_data”: {“text”: “Hello%20world%21” } } } }

Message will look like this:


#5

The example request has "Hello World!" as is. Looks like your case is not an “as needed” situation. I don’t know what case would actually need to be URL encoded.


#6

Yes, it seems you are right. Tested Cyrillic text “Привет” with raw utf8 text and escaped utf8 text ("\u041f\u0440\u0438\u0432\u0435\u0442") and both variants seem to work. Perhaps documentation is wrong then, text actually should not be URL encoded.


#7

Hi ! I too am facing the same issue. It says HTTP 415 (Unsupported Media Type). Following is the sample request. Your help is needed at the earliest @joncipriano

{
  "event": {
    "type": "message_create",
    "message_create": {
      "target": {
        "recipient_id": "XXXXXX"
      },
      "message_data": {
        "text": "Flight SF8020 from San Francisco to Montreal is ahead of schedule and will land in approximately 15 minutes. Can we help with anything else?",
        "ctas": [
          {
            "type": "web_url",
            "label": "See flight details",
            "url": "https://www.myairline.domain/see-flight-details"
          },
          {
            "type": "web_url",
            "label": "Map it",
            "url": "https://www.myairline.domain/map-it"
          },
          {
            "type": "web_url",
            "label": "Visit MyAirline.domain",
            "url": "https://www.myairline.domain/"
          }
        ]
      }
    }
  }
}

#8

Hi @joncipriano ! Please can you look at my post and suggest something .

Thanks


#9

Please be aware that due to the US holiday this week there may be some delay in addressing your question - thanks for your kind understanding!


#10

Thanks @andypiper for letting me know. Will wait for next steps from Twitter Team once they are back in office.


#11

Hi @andypiper !
Please can you request someone to look into the above issues at priority basis. Just update me once it is done.

Thanks


#12

I am reposting my request again

HTTP 415 (Unsupported Media Type). Following is the sample request.

{
  "event": {
    "type": "message_create",
    "message_create": {
      "target": {
        "recipient_id": "XXXXXX"
      },
      "message_data": {
        "text": "Flight SF8020 from San Francisco to Montreal is ahead of schedule and will land in approximately 15 minutes. Can we help with anything else?",
        "ctas": [
          {
            "type": "web_url",
            "label": "See flight details",
            "url": "https://www.myairline.domain/see-flight-details"
          },
          {
            "type": "web_url",
            "label": "Map it",
            "url": "https://www.myairline.domain/map-it"
          },
          {
            "type": "web_url",
            "label": "Visit MyAirline.domain",
            "url": "https://www.myairline.domain/"
          }
        ]
      }
    }
  }
}

Please share if that has been fixed. It is already 19 days.


#13

I’m facing the same issue here.
I’m trying to send messages with the new API at
https://api.twitter.com/1.1/direct_messages/events/new.json

with the following data:
{‘event’: {‘type’: ‘message_create’, ‘message_create’: {‘target’: {‘recipient_id’: ‘230120049’}, ‘message_data’: {‘text’: ‘Hey there’}}}}

But I always get a 415 error code.

When I use the old send PM API (direct_messages/new) I’m able to send the message but this API don’t support quick replies…

Any help appreciated!


#14

I just tried the exact same call using twurl. When I copy/pasted your data into the body of my post, it failed with a 415 Unsupported Media Type. Then I changed the single quote ’ to double quote " throughout the JSON body, and it succeeded. I conclude that your JSON encoding is incorrect.


#15

Thanks @andypiper, there are single quotes because I copy/pasted it from a python print. I thought the python requests library would encode the json body for me in requests.post("https://api.twitter.com/1.1/direct_messages/events/new.json", auth=self.auth, data=data) but I then I realized I had to use the json parameter like this: requests.post("https://api.twitter.com/1.1/direct_messages/events/new.json", auth=self.auth, json=data)

Now it works!

Not related to the Twitter API but leaving this here just in case it helps someone in the future…


#16

Excellent, thanks for sharing the resolution, and glad it is working for you now!