Direct Message post with media returns code 131 (Internal server error)



HI Twitter, how are you today?

I’ve been trying to send a direct message with an image attachment as per guidelines here
and here.

Unfortunately sending message with attached media id does not work. Without media - works just fine.
This is my request (Ruby):

    response = do |req|
      req.headers['Authorization'] = auth_header.oauth_auth_header.to_s
      req.headers['Content-Type'] = 'application/json'
      req.body = message_data.to_json

This is my message_data.to_json json (ids changed on purpose):

            "text":"gogogo image attachment ggg",

response: "{\"errors\":[{\"message\":\"Internal error\",\"code\":131}]}"
Have to remind that same request without attachment part works perfectly fine.
Could someone please help me here?

Many Thanks in advance


When you uploaded the media, were you able to confirm that it succeeded using the STATUS command, before moving on to send the Direct Message?


Thanks Andy, that’s a very good shout. I haven’t confirmed the status actually.
Will add STATUS ensuring block and come back here with some results!

Thanks again


While performing STATUS check stumbled upon another blocker.

After performing the INIT phase as per above I got the media_id:

[4] pry(#<Attachment>)> media_id = JSON.parse(response.body)['media_id_string']
=> "920638646432272385"

when I submit the same id to status check:

    response = Faraday.get do |req|
      req.headers['Authorization'] = auth_header.oauth_auth_header.to_s

I get {"request":"\/1.1\/media\/upload.json","error":"Invalid mediaId."}.

Please note that url is correct:

[1] pry(#<Attachment>)> response
=> #<Faraday::Response:0x007fd2b70db8a8
  #<struct Faraday::Env
   body="{\"request\":\"\\/1.1\\/media\\/upload.json\",\"error\":\"Invalid mediaId.\"}",

Also upload hasn’t expired yet because STATUS request is made in just few seconds after INIT request.
I guess this is why I’m getting 131 on the first place, but how is this possible that media_id is missing seconds after INIT phase?

Going through this Invalid media Id for now


Ha, apparently I overthought all this stuff. Using plain old upload endpoint (not multi-chunk upload) works like a charm. And it doesn’t require all the INIT, APPEND, STATUS checks, will go with that for now.

Thanks again Andy.