Anvil endpoints always returning 500 error

Hello,

I’ve been trying to serve an API endpoint from my app, so I added the appropriate code to my server module:

@anvil.server.http_endpoint("/send_push_notification", methods=["POST"])
def send_push_notification_api():
    try:
        # Get raw request body
        raw_body = anvil.server.request.get_bytes().decode("utf-8")
        print("Raw request body:", raw_body)  # Debugging

        # Parse JSON manually
        data = json.loads(raw_body)
        print("Parsed JSON:", data)  # Debugging

        # Extract parameters
        apartment = data.get("apartment")
        property = data.get("property")
        visit_type = data.get("visit_type", "unknown")
        delay = data.get("delay", 0)

        # Validate input
        if not apartment or not property:
            return {"status": "error", "message": "Missing required parameters."}, 400

        # Call existing function to process the request
        notify_user_of_package_push_task(apartment, property, visit_type, delay)

        return {"status": "success", "message": "Push notification request received."}, 200

    except Exception as e:
        return {"status": "error", "message": str(e)}, 500

And I’m testing it from my terminal at home with a curl command:

curl -X POST "https://xyzmyappurl.anvil.app/debug/BIGSTRING%BIGSTRING2/_/api/send_push_notification" \
     -H "Content-Type: application/json" \
     --data '{"apartment": "1224", "property": "0", "visit_type": "delivery", "delay": 2}'

However the response I get is:
[{"status":"error","message":"'HttpRequest' object has no attribute 'get_bytes'"},500]

I also tried the function simply with
response_data = anvil.server.request.body_json

but I always get:
[{"status":"error","message":"'NoneType' object has no attribute 'get'"},500]**%**

What could I be doing wrong here?

Lol at the app session. You should find a traceback telling you where it crashed and provide some useful information.

The app sessions are blank other than when i print the response_data = anvil.server.request.body_json

And I think the reason they are blank is that the code never even gets past this line:
raw_body = anvil.server.request.get_bytes().decode("utf-8")

Perhaps Anvil doesn’t like how the request is formatted?

Does this help?

Well, I did get it working eventually with a different implementation, namely anvil.server.request.body_json as opposed to anvil.server.request.get_bytes().decode("utf-8"):

@anvil.server.http_endpoint("/send_push_notification", methods=["POST"])
def receive_push_request_api():
    """
    API endpoint that allows external apps to send a push notification
    """
    try:
        request_data = anvil.server.request.body_json
        print(request_data)
      
        apartment = request_data.get("apartment")
        property = request_data.get("property")
        visit_type = request_data.get("visit_type", "default")
        delay = request_data.get("delay", 0)

        if not apartment or not property:
            return {"status": "error", "message": "Missing required parameters."}, 400

        # Trigger the existing background task
        anvil.server.launch_background_task(
            "notify_user_of_package_push", apartment=apartment, property=int(property), visit_type=visit_type, delay=delay
        )

        return {"status": "success", "message": "Push notification request received."}
    
    except Exception as e:
        return {"status": "error", "message": str(e)}, 500

The only problem is that I am not sure why it’s working now, since I believe I tried this method earlier with the same payload and was getting [{"status":"error","message":"'NoneType' object has no attribute 'get'"},500]