#[SOLVED] Appwrite Fucntion req.get('authorization') Not Working

17 messages · Page 1 of 1 (latest)

night void
#

Hello guys, from the api docuation of livekit.io https://docs.livekit.io/realtime/server/webhooks/#Receiving-webhooks, it is stated that to recieve webhook the `req.get('authorization') will need to be called, but calling it in Appwrite Cloud function its outputting

    at Module.default (file:///usr/local/server/src/function/src/main.js:29:48)
    at execute (/usr/local/server/src/server.js:157:48)
    at async action (/usr/local/server/src/server.js:174:13)
    at async /usr/local/server/src/server.js:10:9```

 How can this be resolved?
LiveKit Docs

Configure LiveKit to notify your server when room events take place.

candid cove
#

It looks like it's trying to get the value of the authorization header which I think is done like this

const authorization = req.headers["Authorization"];

I'm not sure if that's 100% correct as I'm trying to do it from memory but the link I sent will give a much better idea as to what you need to be doing

night void
#

okay, thanks

let me check

night void
#

i could not resolved this issue
please, more help is needed...

woven zealot
#

send your function code here and may I see how did you set up the web hook in your livekit for gives us more information

night void
#

here is my function code

import { AccessToken, RoomServiceClient,Room , WebhookReceiver} from 'livekit-server-sdk';
import { headers } from "next/headers";


// This is your Appwrite function
// It's executed each time we get a request
export default async ({ req, res, log, error }) => {

  
  const ENDPOINT                  = process.env.MY_APPWRITE_ENDPOINT;
  const PROJECT_ID                = process.env.APPWRITE_PROJECT_ID;
  const API_KEY                   = process.env.APPWRITE_API_KEY;
  const DATABASE_ID               = process.env.DATABASE_ID;
  const LIVE_KIT_API              = process.env.LIVE_KIT_API;
  const LIVE_KIT_SEC_KEY          = process.env.LIVE_KIT_SEC_KEY;
  const LIVE_KIT_WEBSOCKET        = process.env.LIVE_KIT_WEBSOCKET;

  const client = new Client();
  const functions = new Functions(client);
  const database = new Databases(client);

  client.setEndpoint(ENDPOINT)
    .setProject(PROJECT_ID)
    .setKey(API_KEY);
  

  
  const receiver = new WebhookReceiver(LIVE_KIT_API, LIVE_KIT_SEC_KEY);
  const event = receiver.receive(req.body, req.get('Authorization'));

  log(`All Event ${event}`);
  return res.json({
    motto: 'Build like a team of hundreds_',
    learn: 'https://appwrite.io/docs',
    connect: 'https://appwrite.io/discord',
    getInspired: 'https://builtwith.appwrite.io',
  });
};```
gritty sandal
night void
woven zealot
#

Please be note that the req.get is from the express framework itself:

Refer to this documentation:
https://www.geeksforgeeks.org/express-js-req-get-function/
If you read the documentation above. The purpose of the req.get is returns the specified HTTP request header

Now, since we used the cloud function of appwrite, the question here. "What could be the alternative way to get the headers in appwrite cloud function>".

refer to this documentation:
https://appwrite.io/docs/products/functions/develop#request

If you read the documentation you can see the req.headers. It means this is the comparison to get the headers from the http request.

Master serverless function development with Appwrite. Learn how to write and test functions locally, debug code, and optimize for efficient execution.

night void
#

i'm getting this

    at Module.default (file:///usr/local/server/src/function/src/main.js:29:48)
    at execute (/usr/local/server/src/server.js:157:48)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async action (/usr/local/server/src/server.js:174:13)
    at async /usr/local/server/src/server.js:10:9```
#
import { AccessToken, RoomServiceClient,Room , WebhookReceiver} from 'livekit-server-sdk';

export default async ({ req, res, log, error }) => {

  
  const ENDPOINT                  = process.env.MY_APPWRITE_ENDPOINT;
  const PROJECT_ID                = process.env.APPWRITE_PROJECT_ID;
  const API_KEY                   = process.env.APPWRITE_API_KEY;
  const DATABASE_ID               = process.env.DATABASE_ID;
  const LIVE_KIT_API              = process.env.LIVE_KIT_API;
  const LIVE_KIT_SEC_KEY          = process.env.LIVE_KIT_SEC_KEY;
  const LIVE_KIT_WEBSOCKET        = process.env.LIVE_KIT_WEBSOCKET;

  const client = new Client();
  const functions = new Functions(client);
  const database = new Databases(client);

  client.setEndpoint(ENDPOINT)
    .setProject(PROJECT_ID)
    .setKey(API_KEY);
  

  
  const receiver = new WebhookReceiver(LIVE_KIT_API, LIVE_KIT_SEC_KEY);
  const event = receiver.receive(req.body, req.headers('Authorization'));

  log(`All Event ${event}`);
  return res.json({
    motto: 'Build like a team of hundreds_',
    learn: 'https://appwrite.io/docs',
    connect: 'https://appwrite.io/discord',
    getInspired: 'https://builtwith.appwrite.io',
  });
};```
gritty sandal
night void
#

@gritty sandal i'm tryting to access the headers like this

  log(headers); 
  var auth    = headers.authorization;
  log(`Auth ${auth}`);```

from the log output `headers` is outputted but `auth` is logging out `undefined`
candid cove
#

That should give you the value you're looking for

night void
#

thanks @candid cove @woven zealot @gritty sandal