POST to upload/media fails often with "media type unrecognized"


#1

I am POSTing with base64-encoded data in the media_data parameter to media/upload. This usually works fine, but there are some images that seem to sometimes succeed and sometimes fail.

Here’s some example s3 buckets pointing to JPEG images that seem to fail:
https://percolate.com/cdn/library/image/60304017
https://percolate.com/cdn/library/image/58461991

This one uploaded fine, even though I have confirmed it is byte-for-byte identical to the second image above:
https://percolate.com/cdn/ugc/image/raw/e9ed458e66c0b726c7b713adf7711f4d.jpg

Any hints what might be wrong here? The fact that it usually works - and sometimes even works with exactly the same data - makes me suspect a problem on Twitter’s side.


#2

Image 60304017 responds with a generic Content-Type of binary/octet-stream; image 58461991 responds with image/jpeg.

The bodies might be byte-for-byte identical but it looks like key headers vary.


#3

True, but that header has no bearing on what we send to twitter. We load the bytestream, base64-encode it, and POST to media/upload. The content-type returned by those S3 objects doesn’t get used anywhere in our upload process.


#4

I’ve been playing around with this Python code sample:

In using python-twitter and the “media” parameter for the below sample code, it seems to upload properly.

Can you try either:

1.) using the media parameter and seeing if that works,
2.) ensure you’re doing chunking for video (it’s required)
3.) Trying the below code to see if it works

For chunking the code is available here in python:


#5

FWIW: I have the code to test your own file types (and see any errors) on Heroku:

http://django-rest-apis.herokuapp.com/media/video

I attempted all your files – in both “media” and “media_data” modes, and they uploaded and attached to a tweet fine.

I’m sure there’s something I’m missing, but give your files an upload yourself, and feel free to download the code and test it out to see what I’m missing:


#6

@rchol thanks very much for the replies. I’ve been continuing to try to reproduce this - no luck so far. It’s odd, because we’re still seeing the symptoms in production logs with customer data. I’ll keep at it.


#7

@rchoi I still haven’t been able to reproduce this at will, but found a likely culprit on our side … before upload, the image data is fetched via an old library that assumed urllib2.urlopen(...).read() is guaranteed to return all the data, which is false. Anytime that assumption is violated, we’d be uploading truncated data. If I manually truncate the data to an arbitrary length, I have verified that Twitter (sometimes) gives this “media type unrecognized” response.

I’m going to push out a fix for this and watch the logs to see if this solves the problem for good. Optimistic, but we’ll see :smile:

Thanks again. I’ll report back here with my results.


#8

Glad to hear that you may have pinpointed the issue. Is the library in question internal to your team or is it public?


#9

Internal.


#10

Ok, thanks. Let us know if your change makes a difference when pushed.


#11

We are pretty sure nothing is being truncated now, but still seeing this issue occasionally. Will follow up again if we find a consistent recipe to reproduce it, eg. with twurl.


#12

Here is an example that fails consistently:

We also tried posting the Tweet natively (not through the API), and it fails with the error:
“Uploading photo failed! Please try again later.”

The image doesn’t seem to be corrupt, but it wouldn’t surprise me. Any ideas?


#13

Going back and forth with the Media Platform team. The one odd thing about this image to me is that it doesn’t have a color space or embedded color profile. I’m working to confirm if this would through an “unsupported” error.


#14

Upon further inspection, the team is getting the following message when parsing headers:
invalid TIFF header in EXIF data

This is causing the entire header parsing to fail. The rest of the image is fine though, so you may just be able to correct the EXIF data and try again. The team will investigate a way to get this messaging back to the user.


131 "Internal Error" when creating a PNG image tweet
#15

@jbulava Thanks. Was that for the images I originally linked to in my original report, or the one Brian Chance commented about on jan 6th? Do they all fail the same way?


"Internal error" code 131 when creating tweet with PNG images via REST API
#16

@jbulava did you guys make relevant fixes recently? I just tried my bad PNG and Brian’s bad JPEG from this thread, and both worked fine - both natively and via API! :grinning:


#17

@slinkp23, my response was indeed referring to Brian’s image. The media platform team did mention that they may be able to clean an image’s exif data rather than forcing the upload to fail, so this might have been shipped last week.


#18

Sorry to hi-jack this, I am having a similar issue here: 131 "Internal Error" when creating a PNG image tweet

Wonder if this is related or not, and can the media platform team take a look on those images to see what’s causing the issue?

Thanks.


#19

Hi Guys.
For over a year I’ve not been able to add more than 3 photographs to a tweet. I just get “Uploading Photo failed, please try again later” But I try and try and try and try… and never get anywhere. Have tweeted support numerous times but never get a response. Is there anything anyone can do here?