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

Document passing body via client.request() (using query and headers['Content-Type']) #186

Open
karlhorky opened this issue Jul 29, 2023 · 5 comments

Comments

@karlhorky
Copy link
Contributor

karlhorky commented Jul 29, 2023

Hi there, thanks for this library!

I'm just trying to pass a "Body" option active: true to a new thumbnail, which is mentioned in the API documentation for POST https://api.vimeo.com/videos/{video_id}/pictures (see below)

Screenshot 2023-07-29 at 18 57 30

The numbered comments in the code block below show all of the *non-working* things I've tried (I tried them one by one, on their own):

import { Vimeo } from '@vimeo/vimeo';

const client = new Vimeo(VIMEO_CLIENT_ID, VIMEO_CLIENT_SECRET, VIMEO_ACCESS_TOKEN);

client.request(
  {
    method: 'POST',
    // 1. Pass via query
    path: `/videos/${vimeoVideoId}/pictures?active=true`,

    // 2. Pass via `body` string
    body: JSON.stringify({ active: true }),

    // 3. Pass via `body` object
    body: { active: true },

    // 4. Pass via `query`
    query: { active: true },

    // 5. Pass via `params`
    params: { active: true },    
  },
  (error, response) => {
    if (error) {
      console.log('error', error);
    }

    console.log('response', response);
  },
);

I don't see the body documented anywhere... is there a way to do this with the client?

@karlhorky
Copy link
Contributor Author

karlhorky commented Jul 29, 2023

The closest thing I can see is this in the tests:

it('sends body as JSON if content type is application/json', async () => {
await vimeo.request({ method: 'POST', path: '/path', query: { a: 'b' }, headers: { 'Content-Type': 'application/json' } })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ body: '{"a":"b"}' }))
})

But using headers['Content-Type'] of 'application/json' with a query object also does not set the new thumbnail to active: true 😬

import { Vimeo } from '@vimeo/vimeo';

const client = new Vimeo(VIMEO_CLIENT_ID, VIMEO_CLIENT_SECRET, VIMEO_ACCESS_TOKEN);

client.request(
  {
    method: 'POST',
    path: `/videos/${vimeoVideoId}/pictures`,
    headers: { 'Content-Type': 'application/json' },
    query: { active: true },
  },
  (error, response) => {
    if (error) {
      console.log('error', error);
    }

    console.log('response', response);
  },
);

@karlhorky
Copy link
Contributor Author

karlhorky commented Jul 29, 2023

Ohh, it seems that it's an unusual behavior of the Vimeo API:

⚠️⚠️ If you don't specify time along with active in the body, then active is silently ignored 🤔

Screenshot 2023-07-29 at 18 57 30

Solution code incoming (I will also link to the few other posts which are asking how to create thumbnails)

@karlhorky
Copy link
Contributor Author

Add a new thumbnail at 0 seconds of a video

If you uploaded a new video, and want to set a new thumbnail at 0 seconds of the video:

import { Vimeo } from '@vimeo/vimeo';

const client = new Vimeo(VIMEO_CLIENT_ID, VIMEO_CLIENT_SECRET, VIMEO_ACCESS_TOKEN);

client.request(
  {
    method: 'POST',
    path: `/videos/${vimeoVideoId}/pictures`,
    headers: { 'Content-Type': 'application/json' },
    query: {
      active: true, // Make this the active thumbnail
      time: 0, // Time offset in seconds from start of video
    },
  },
  (error, response) => {
    if (error) {
      console.log('error', error);
    }

    console.log('response', response);
  },
);

@karlhorky karlhorky changed the title Pass body via client.request() Document passing body via client.request() (using query and headers['Content-Type']) Jul 29, 2023
@karlhorky
Copy link
Contributor Author

I renamed this issue to be about documentation, because this approach of passing the body via the query property and needing to also set headers['Content-Type'] to 'application/json' was pretty hidden.

This basic use case should probably be documented front and center on the documentation for this library.

Maybe on Vimeo Developer: Node.js Library Examples

@filipegorges
Copy link

So many hours lost trying to figure this out, thank you, it's a shame that you brought this up over a year ago and it's still not updated on the documentation

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