Discord注册API返回50035(Invalid Form Body)错误,如何修复?
Let's break down what's going wrong here and get your API call working properly.
1. You're Putting Request Body Data in Headers (And Using the Wrong Parameter)
The biggest issue is that you're passing registration fields like email, password, and username in the headers instead of the request body. Discord's /auth/register endpoint expects these values in a JSON-formatted body, not as HTTP headers.
When you used the data parameter with requests.post(), it encodes the data as application/x-www-form-urlencoded (form data), which doesn't match what Discord's API is expecting. That's exactly why you're getting the Invalid Form Body error (code 50035).
2. Missing Critical Headers & Incorrect Data Types
Discord's API also requires specific headers to accept your request, and some fields need the right data type (like consent should be a boolean, not a string).
Corrected Code
Here's the fixed version of your code with all these issues addressed:
import requests # Assume these variables are properly defined elsewhere email = "your_valid_email@example.com" password = "your_strong_password" username = "your_desired_username" # Required headers for Discord API headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36", "Content-Type": "application/json" } # Registration payload (this goes in the request body, not headers) payload = { "consent": True, # Use boolean instead of string "true" "date_of_birth": "2000-08-05", "email": email, "password": password, "username": username } response = requests.post( "https://discord.com/api/v9/auth/register", headers=headers, json=payload # Use json parameter to send JSON-formatted body ) print(f"Status Code: {response.status_code}") print(response.text)
Key Fixes Explained:
- Moved registration data to
payload: This is the correct place for user registration details, not HTTP headers. - Used
json=payloadinstead ofdata: This tellsrequeststo send the body as JSON and automatically sets the correctContent-Type(though we explicitly set it too for clarity). - Fixed
consenttype: Changed from string"true"to booleanTrue—Discord's API strictly validates data types for this field. - Added
User-Agentheader: Discord blocks requests without a valid user agent (it looks like a bot otherwise). Using a browser-like user agent helps avoid this.
If You Still Get Errors After This:
Discord's registration process has additional safeguards that might block your request:
- Missing
fingerprint: The API often requires afingerprintfield (a unique identifier generated by the Discord client). You can grab this from your browser's developer tools when registering manually. - CAPTCHA Verification: Discord now requires human verification for most new registrations. Automating this is tricky, as you'd need to integrate a CAPTCHA-solving service (which violates Discord's Terms of Service in most cases).
内容的提问来源于stack exchange,提问作者WagonToZiomal

