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
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:
It will force your application to append the
Content-Type
on each request.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
en public function __construct(array $settings) añade la inicialización.
en public function performRequest($return = true, $curlOptions = array())
remplaza
por
para finalizar usa:
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:
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 withoutdumps'ing
it.This might not really help but just check your data integrity.