Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send new direct message through Twitter API https://api.twitter.com/1.1/direct_messages/events/new.json?

I am implementing Twitter new Direct message API, https://api.twitter.com/1.1/direct_messages/events/new.json

I am facing error, 415 Unsupported Media Type

I am able to call it via TWURL tool and currently I am sending simple text message through it.

From error code, I understand that either there is content-type issue or payload is in incorrect format.

I am passing payload as,

options = {
      "event": {
        "type": "message_create",
        "message_create": {
          "target": {
            "recipient_id": "1234"
          },
          "message_data": {
            "text": "test"
          }
        }
      }
    }

Payload is converted to usual Ruby hash, i.e. :key => "value"

{:event=>
  {:type=>"message_create",
   :message_create=>
    {:target=>{:recipient_id=>"1234"},
     :message_data=>{:text=>"test"}}}}

How to preserve third party API request format?

Any suggestion will be great help.

Thanks

like image 680
Mansi Shah Avatar asked Apr 29 '26 20:04

Mansi Shah


1 Answers

Have you tried setting Content-Type ('application/json')? in your content header before sending?

It is one of the most common issues.

You can do so by doing something similar to:

before_filter :set_content_type
  def set_content_type
    @headers["Content-Type"] = "application/json; charset=utf-8"
  end

It will force your application to append the Content-Type on each request.

like image 100
mutantkeyboard Avatar answered May 01 '26 12:05

mutantkeyboard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!