Skip to content

Conversation

@teifip
Copy link

@teifip teifip commented Jan 8, 2018

There are multiple open issues asking support for POST requests with JSON body (as needed to use the new POST direct_messages/events/new API). Pull request #230 addresses this problem, but it seems to have ended up stuck.

This pull request tries to address the problem while ensuring complete backward compatibility with any existing use the the node-twitter library. With the updated code, when the client.post convenience method is used to submit a JSON body, the params object must be structured as follows:

{
  json_body: {
    // JavaSctript object to be submitted as JSON body
  }
}

For example:

let params = {
  json_body: {
    event: {
      type: 'message_create',
      message_create: {
        target: { recipient_id: '1234567890' },
        message_data: { text: 'Hi there!' }
      }
    }
  }
};

client.post('direct_messages/events/new', params, (error, result, response) => {
  if (error) throw error;
  console.log(result);  // Newly created event object.
  console.log(response);  // Raw response object.
});

Note that when the POST request is passed to the request module with the indication to use JSON body, the request module automatically parses the response body. Therefore, the code has been updated to avoid attempting to parse the data returned by the request module if the data is an object rather than a string.

Added support for POST requests with JSON body. See updated README.md for usage information.
@peterpme
Copy link

Hey @teifip!

This is awesome work!! I'm so happy to see folks improving the library. I was wondering if you'd be interested in helping with Twitter Lite:
https://github.com/Preposterous/twitter-lite

  • 591 bytes of source code
  • fully featured API
  • up-to-date
  • maintained for the foreseeable future
  • client / server support (switching client lib to crypto-js in 5 min)

I would be so happy if you helped me incorporate this into the repo.

Thanks!

@bobber205
Copy link

Is this ever gonna be merged? This is a really important feature.

@peterpme
Copy link

@marqsitelabs
Copy link

Nice work @teifip. This pull request should be considered @desmondmorris

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants