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

Example of post request with JSON parameters? #36

Open
cherdt opened this issue Feb 27, 2018 · 2 comments
Open

Example of post request with JSON parameters? #36

cherdt opened this issue Feb 27, 2018 · 2 comments

Comments

@cherdt
Copy link

cherdt commented Feb 27, 2018

In the POST example for a static update, the payload consists of simple key-value pairs:

status='Hello @pybirdy!'

I've successfully created a direct message using the deprecated direct_messages/new endpoint:

response = self.client.api.direct_messages.new.post(screen_name='cherdt', text='hello there')

I'm unclear how to accomplish the same using the direct_messages/events/new endpoint (https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event).

I've tried creating a string representing the JSON, but I've been getting the following error:

birdy.twitter.TwitterApiError: Unable to decode JSON response.

Here's an example request I tried that produced the error:

response = self.client.api.direct_messages.events.new.post(event='{"type": "message_create", "message_create": {"target": {"recipient_id": "6039192"}, "message_data": {"text": "hi there"}}}')

@sect2k
Copy link
Contributor

sect2k commented Feb 27, 2018

Passing parameters as JSON is something new to Twitter API, as it wasn't an option when birdy was written and it looks like we can't currently handle this use case.

I have a general idea how to handle this and should have a new version out in a day or two, time permitting.

Thanks for bringing this to my attention, haven't really worked with twitter API in a while, so this change went unnoticed.

@sect2k
Copy link
Contributor

sect2k commented Feb 27, 2018

@cherdt can you check out this branch (features/json-params) and let me know if it works. I don't have time to test this fully, but a quick test with requestb.in indicates that it should.

For API methods that require JSON params, the syntax is:

params = {
    "type": "message_create",
    "message_create":  {
        "target": {
            "recipient_id": "6039192"
        },
        "message_data": {
            "text": "hi there"
        }
    }
}

response = self.client.api.direct_messages.events.new.post(params)

You just pass a dict to post() and birdy will handle the rest.

If all is OK, i'll release a new version ASAP.

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

No branches or pull requests

2 participants