Media upload fails if charset is specified

bug
media-upload
api

#1

Hi all!
I believe that there is a bug in how the server processes the media upload requests (both chunked and unchunked versions): it normally works, but if the application specifies a charset in the Content-Type header, then it fails with the usual error:

{"errors":[{"code":38,"message":"media parameter is missing."}]}

I’ve tried the upload many times, and I’ve verified that it fails whenever I change the header from

Content-Type: multipart/form-data, boundary=...blabla...

to

Content-Type: multipart/form-data, boundary=...blabla...;charset=UTF8

Unfortunately, the charset=UTF-8 part is automatically added by the XMLHttpRequest.send() method (at least, in its QML implementation), so I don’t have control over that. Can some twitter dev please double check this?


#2

After investigating the issue more, I realized that the reason why this failed is that I was using two different separators in the Content-Type line; and indeed, changing the header from
Content-Type: multipart/form-data, boundary=...blabla...;charset=UTF8
to
Content-Type: multipart/form-data; boundary=...blabla...;charset=UTF8
(note how the comma before boundary gets changed into a semicolon) was enough to make the upload succeed. :slight_smile:


#3

Great to hear you got it working, thanks for sharing the solution!


#4

I am having the same problem. Can you share de code?

uploadimage: function (img, callback) {
        var fs = require('fs');
        var b64content = fs.readFileSync('img/farobot4.gif', { encoding: 'base64' })
       
        var request_options = {
            url: 'https://upload.twitter.com/1.1/media/upload.json',
            oauth: twitter_oauth,
            media_data: b64content,
            json: true,
            headers: {
               // 'content-type': 'application/x-www-form-urlencoded',
                'content-type':'multipart/form-data'
               // 'Content-Transfer-Encoding': 'base64'
            },
           
            
        }
        request.post(request_options, function (error, response, body) {

        if (error) {
            console.log('Error creating welcome message.')
            console.log(error)
            return
        }
        callback(null, { 'base64': body });
    })