Back

bodyJson does not exist

  • 0
  • Functions
  • Cloud
Raspova
20 Dec, 2024, 00:35

What did I do wrong here, I just try to access some variable from JSON I send via Post request It says it doesn't exist

TypeScript
from appwrite.client import Client
from appwrite.services.users import Users
from appwrite.exception import AppwriteException
from appwrite.services.messaging import Messaging
import os
import json

def main(context):
    try:
        if context.req.path == "/send_email":
            client = (
                Client()
                .set_endpoint(os.environ["APPWRITE_FUNCTION_API_ENDPOINT"])
                .set_project(os.environ["APPWRITE_FUNCTION_PROJECT_ID"])
                .set_key(context.req.headers["x-appwrite-key"])
            )
            messaging = Messaging(client)
            #user = context.req.bodyJson.get('user_id')
            #subject = context.req.bodyJson.get('subject')
            #content = context.req.bodyJson.get('content')
            context.log(context.req.bodyJson)
            context.log(context.req)
            return context.res.json( {
                #"user": user,
                #"subject": subject,
                "content": json.dumps(context.req.bodyJson)
            })

Traceback (most recent call last): File "/usr/local/server/src/server.py", line 106, in action output = await asyncio.wait_for(execute(context), timeout=safeTimeout) File "/usr/local/lib/python3.9/asyncio/tasks.py", line 479, in wait_for return fut.result() File "/usr/local/server/src/server.py", line 99, in execute output = userModule.main(context) File "/usr/local/server/src/function/src/main.py", line 22, in main context.error(context.req.bodyJson) AttributeError: 'Request' object has no attribute 'bodyJson'

TL;DR
The developer is getting an error because they are trying to access 'bodyJson' from the 'Request' object, which does not exist. To access the JSON sent via a Post request, the developer should use 'context.req.json' instead of 'bodyJson'. This will allow them to access the JSON data successfully.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more