Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Send status updates as `multipart/form-data`

The `request` library uses `querystring.stringify` to encode the `POST` body,
which leave parenthesis `()` alone, but Twitter appears to expect them
percent-encoded (and http://en.wikipedia.org/wiki/Percent-encoding) appears to
agree that they should be). When you send them this way, you get an impressively
unhelpful error response:

    {
      "code": 32,
      "message": "Could not authenticate you"
    }

Twitter doesn't list code 32 at
https://dev.twitter.com/docs/error-codes-responses, and the message matches
error 135, which tells you your timestamp is wrong.

In order to set the `content-type` as `multipart/form-data` instead of
`multipart/x-www-form-urlencoded`, we could break out the `form` parameter from
the `request` call:

    var req = request.post(url);
    var form = req.form();
    form.append('status', data.body);

But the shared `post` function isn't set up to do that. So instead, we can build
the body ourselves, which is a real shame but works consistently across
services.

cc @bryancallahan
  • Loading branch information...
commit 9911fcb263124e5d4e54eac70daa8e57311bdfbb 1 parent 33f042f
@kristjan kristjan authored
Showing with 8 additions and 4 deletions.
  1. +8 −4 lib/services/twitter/posting.js
View
12 lib/services/twitter/posting.js
@@ -14,7 +14,7 @@ function post(endpoint, data, params, callback) {
token: data.auth.token.oauth_token,
token_secret: data.auth.token.oauth_token_secret
};
- var url = TWITTER_API_BASE + endpoint + '.json'
+ var url = TWITTER_API_BASE + endpoint + '.json';
request.post(url, params, function(err, resp, body) {
if (err) {
err = JSON.parse(err.data).error;
@@ -31,9 +31,13 @@ function post(endpoint, data, params, callback) {
function postStatus(data, callback) {
post('/statuses/update', data, {
- form: {
- status: data.body
- }
+ headers: { 'content-type': 'multipart/form-data' },
+ multipart: [
+ {
+ 'Content-Disposition': 'form-data; name="status"',
+ body: data.body || ''
+ }
+ ]
}, callback);
}

0 comments on commit 9911fcb

Please sign in to comment.
Something went wrong with that request. Please try again.