Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for POST with JSON body #278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -114,6 +124,28 @@ client.post('statuses/update', {status: 'I Love Twitter'}, function(error, twee
});
```

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

The REST API convenience methods will also return Promises if:
Expand Down
19 changes: 12 additions & 7 deletions lib/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,19 @@ Twitter.prototype.__request = function(method, path, params, callback) {
options.qs = params;
}

// Pass form data if post
// Pass either form data or JSON body if post
if (method === 'post') {
var formKey = 'form';
if (params.json_body) {
options.json = params.json_body;

if (typeof params.media !== 'undefined') {
formKey = 'formData';
} else {
var formKey = 'form';

if (typeof params.media !== 'undefined') {
formKey = 'formData';
}
options[formKey] = params;
}
options[formKey] = params;
}

// Promisified version
Expand All @@ -158,7 +163,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 +201,7 @@ Twitter.prototype.__request = function(method, path, params, callback) {
if (data === '') {
data = {};
}
else {
else if (typeof data === 'string') {
data = JSON.parse(data);
}
}
Expand Down