Consider a webhook payload validation function that checks if the payload contains the required fields event and timestamp. What will be the output of the following code when given the payload {"event": "user_signup", "timestamp": 1680000000}?
def validate_payload(payload): required_fields = ['event', 'timestamp'] for field in required_fields: if field not in payload: return f"Missing field: {field}" return "Payload valid" payload = {"event": "user_signup", "timestamp": 1680000000} print(validate_payload(payload))
Check if all required keys exist in the payload dictionary.
The function checks for both 'event' and 'timestamp' keys. Since both are present, it returns 'Payload valid'.
Why do webhook payloads often include a signature or hash value?
Think about how the receiver can trust the source of the webhook.
The signature allows the receiver to verify that the payload was sent by a trusted source and was not altered during transmission.
Given the following Python code that parses a webhook payload, what error will it raise?
import json def parse_payload(payload_str): data = json.loads(payload_str) return data['user']['id'] payload = '{"user": null}' print(parse_payload(payload))
Consider what happens when you try to access a key on a NoneType object.
The 'user' key exists but its value is null (None in Python). Trying to access ['id'] on None causes a TypeError.
Which of the following strings is a valid JSON payload representing a webhook event with nested user data?
Remember JSON requires double quotes for keys and string values.
Option D uses double quotes correctly for keys and string values. Other options use single quotes or unquoted keys/values, which are invalid in JSON.
Given the following webhook payload, how many top-level keys does it contain?
{
"event": "payment_received",
"timestamp": 1680001234,
"data": {
"payment_id": "abc123",
"amount": 49.99,
"currency": "USD"
},
"metadata": {
"customer_id": "cust789",
"notes": "First payment"
}
}Count only the keys at the outermost level of the JSON object.
The top-level keys are 'event', 'timestamp', 'data', and 'metadata', totaling 4 keys.