node-twitter icon indicating copy to clipboard operation
node-twitter copied to clipboard

Support for POST with JSON body

Open teifip opened this issue 6 years ago • 4 comments

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.

teifip avatar Jan 08 '18 23:01 teifip

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!

peterpme avatar Feb 16 '18 06:02 peterpme

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

bobber205 avatar Apr 19 '18 23:04 bobber205

@bobber205 see https://github.com/Preposterous/twitter-lite

peterpme avatar Apr 19 '18 23:04 peterpme

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

marqmarti avatar Sep 27 '18 22:09 marqmarti