skip to Main Content

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

3

Answers


  1. 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.

    Login or Signup to reply.
  2. https://github.com/J7mbo/twitter-api-php/blob/master/TwitterAPIExchange.php

    usa la clase TwitterAPIExchange y aplica estos cambios.

    Yo lo solucione modificando la clase así:

    a la clase TwitterAPIExchange , añade la propiedad

    public $appjson;
    

    en public function __construct(array $settings) añade la inicialización.

    $this->appjson=false;
    

    en public function performRequest($return = true, $curlOptions = array())

    remplaza

    $header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
    

    por

    if ($this->appjson) {
    $header = array('Content-Type: application/json',$this->buildAuthorizationHeader($this->oauth), 'Expect:');
    } else {
    $header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
    }
    

    para finalizar usa:

    .....
    $twitter = new TwitterAPIExchange($settings);
    ......
    ......
    $twitter->appjson=true;
    $twitter->buildOauth($url, $requestMethod)
    ->performRequest(true,
    array( CURLOPT_POSTFIELDS => $params)
    );
    
    Login or Signup to reply.
  3. I got similar error and it turned out the data format I was sending the request in was a bit wrong. I was using python requests with the following headers:

    HEADERS = {
        'Content-type': 'application/json',
        'Accept': 'application/json'
    }
    

    But still using json.dumps() on the actual data, the request was subsequently inconsistent with bad data, it all worked when I just sent the data without dumps'ing it.

    This might not really help but just check your data integrity.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search