Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
'curly': 2,
'eqeqeq': 2,
'max-depth': 2,
'max-statements': [2, 20],
'max-statements': [2, 22],
'new-cap': 2,
'no-caller': 2,
'no-cond-assign': 2,
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ client.post(path, params, callback);
client.stream(path, params, callback);
```

When the `post` convenience method is used to submit a JSON body, the `params` object must be structured as follows:

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

## REST API

You simply need to pass the endpoint and parameters to one of convenience methods. Take a look at the [documentation site](https://dev.twitter.com/rest/public) to reference available endpoints.
Expand All @@ -113,6 +123,28 @@ client.post('statuses/update', {status: 'I Love Twitter'}, function(error, twee
console.log(response); // Raw response object.
});
```
Example of POST with JSON body (new [direct_messages/events/new](https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event) API):

```javascript
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.
});
```


### Promises

Expand Down
20 changes: 12 additions & 8 deletions lib/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,18 @@ Twitter.prototype.__request = function(method, path, params, callback) {
options.qs = params;
}

// Pass form data if post
// Pass form data (or JSON boy if specified) if post
if (method === 'post') {
var formKey = 'form';

if (typeof params.media !== 'undefined') {
formKey = 'formData';
if(params.json_body){
options.json = params.json_body;
}
else{
var formKey = 'form';
if (typeof params.media !== 'undefined') {
formKey = 'formData';
}
options[formKey] = params;
}
options[formKey] = params;
}

// Promisified version
Expand All @@ -158,7 +162,7 @@ Twitter.prototype.__request = function(method, path, params, callback) {
if (data === '') {
data = {};
}
else {
else if (typeof data === 'string') {
data = JSON.parse(data);
}
}
Expand Down Expand Up @@ -196,7 +200,7 @@ Twitter.prototype.__request = function(method, path, params, callback) {
if (data === '') {
data = {};
}
else {
else if(typeof data === 'string'){
data = JSON.parse(data);
}
}
Expand Down