livekit / python-sdks

LiveKit real-time and server SDKs for Python
https://docs.livekit.io
Apache License 2.0
168 stars 49 forks source link

"Unsupported method GET (only POST is allowed)" #248

Closed AyobamitheTitan closed 2 months ago

AyobamitheTitan commented 2 months ago

I was interacting with the RoomService class from livekit.api.room_service. Below is an excerpt from my code:

  async def delete_room(self, room_name: str):
          try:
              room = await self.room_service.delete_room(
                  proto_room.DeleteRoomRequest(
                      room=room_name
                  )
              )
              return room
          except Exception as exc:
              raise Exception(exc)

But then i got this error message:

Traceback (most recent call last):
  File "/Users/pomegranaT/Work/marketplace-rest/src/lives/services/livekit.py", line 24, in create_room
    await self.room_service.create_room(
  File "/Users/pomegranaT/Work/marketplace-rest/env/lib/python3.10/site-packages/livekit/api/room_service.py", line 19, in create_room
    return await self._client.request(
  File "/Users/pomegranaT/Work/marketplace-rest/env/lib/python3.10/site-packages/livekit/api/twirp_client.py", line 103, in request
    raise TwirpError(error_data["code"], error_data["msg"])
livekit.api.twirp_client.TwirpError: ('bad_route', 'unsupported method "GET" (only POST is allowed)')

So i did some debugging and i went into the /livekit/api/twirp_client.py file and i modified this section

    async def request(
        self,
        service: str,
        method: str,
        data: Message,
        headers: Dict[str, str],
        response_class: Type[T],
    ) -> T:
        url = f"{self.host}/{self.prefix}/{self.pkg}.{service}/{method}"
       # print(url) displays https://first-room-9g83dxi6.livekit.cloud//twirp/livekit.RoomService/twirp/livekit.RoomService/CreateRoom
        headers["Content-Type"] = "application/protobuf"

to this:

    async def request(
        self,
        service: str,
        method: str,
        data: Message,
        headers: Dict[str, str],
        response_class: Type[T],
    ) -> T:
        url = f"{self.host}/{method}"
        # print(url) displays https://first-room-9g83dxi6.livekit.cloud/twirp/livekit.RoomService/CreateRoom
        headers["Content-Type"] = "application/protobuf"

With this modification I was able to successfully create a room and get this response:

sid: "RM_YYmecq6kADxJ"
name: "wivonw"
empty_timeout: 300
creation_time: 1725631584
enabled_codecs {
  mime: "video/H264"
}
enabled_codecs {
  mime: "video/VP8"
}
enabled_codecs {
  mime: "video/VP9"
}
enabled_codecs {
  mime: "video/AV1"
}
enabled_codecs {
  mime: "audio/red"
}
enabled_codecs {
  mime: "audio/opus"
}
version {
  unix_micro: 1725631584914699
}
departure_timeout: 20

This is my first time using the sdk in this capacity. I would appreciate some assistance. Thanks in advance for your help

davidzhao commented 2 months ago

Hi there, I think the issue is that you've specified a trailing slash when specifying the URL. This is causing the twirp server to force a redirect. The workaround now is to just remove the trailing slash when specifying the URL.

However, we'll take a look on our side how to prevent this from happening as well.

davidzhao commented 2 months ago

We'll handle this on the server side with https://github.com/livekit/livekit/pull/2988