Roblox商城资产购买脚本出现500内部服务器错误,如何添加json.dumps()并修复问题
Problem Description
I've written a script to purchase assets from the Roblox Marketplace, but it's returning a 500 Internal Server Error. Someone suggested using
json.dumps()might fix it, but I'm not sure how to implement it correctly. Here's my current code:
import re from requests import post, get cookie = "blablabla" ID = 1562150 # getting x-csrf-token token = post("https://auth.roblox.com/v2/logout", cookies={".ROBLOSECURITY": cookie}).headers['X-CSRF-TOKEN'] print(token) # getting item details detail_res = get(f"https://www.roblox.com/library/{ID}") text = detail_res.text productId = int(get(f"https://api.roblox.com/marketplace/productinfo?assetId={ID}").json()["ProductId"]) expectedPrice = int(re.search("data-expected-price=\"(\d+)\"", text).group(1)) expectedSellerId = int(re.search("data-expected-seller-id=\"(\d+)\"", text).group(1)) headers = { "x-csrf-token": token, "content-type": "application/json; charset=UTF-8" } data = { "expectedCurrency": 1, "expectedPrice": expectedPrice, "expectedSellerId": expectedSellerId } buyres = post(f"https://economy.roblox.com/v1/purchases/products/{productId}", headers=headers, data=data, cookies={".ROBLOSECURITY": cookie}) if buyres.status_code == 200: print("Successfully bought item")
Solution
The root cause of the 500 error is that your request body isn't properly formatted as JSON. The Roblox API expects a JSON string, but you're passing a raw Python dictionary directly to the data parameter of the post method. Here are two ways to fix this:
Option 1: Use json.dumps() (as you were advised)
First, you need to import the json module. Then, convert your data dictionary into a JSON string using json.dumps() before passing it to the data parameter. This matches the content-type header you've already set.
Modified code:
import re import json # Add this line to import the json module from requests import post, get cookie = "blablabla" ID = 1562150 # getting x-csrf-token token = post("https://auth.roblox.com/v2/logout", cookies={".ROBLOSECURITY": cookie}).headers['X-CSRF-TOKEN'] print(token) # getting item details detail_res = get(f"https://www.roblox.com/library/{ID}") text = detail_res.text productId = int(get(f"https://api.roblox.com/marketplace/productinfo?assetId={ID}").json()["ProductId"]) expectedPrice = int(re.search("data-expected-price=\"(\d+)\"", text).group(1)) expectedSellerId = int(re.search("data-expected-seller-id=\"(\d+)\"", text).group(1)) headers = { "x-csrf-token": token, "content-type": "application/json; charset=UTF-8" } data = { "expectedCurrency": 1, "expectedPrice": expectedPrice, "expectedSellerId": expectedSellerId } # Convert data to JSON string with json.dumps() buyres = post(f"https://economy.roblox.com/v1/purchases/products/{productId}", headers=headers, data=json.dumps(data), cookies={".ROBLOSECURITY": cookie}) if buyres.status_code == 200: print("Successfully bought item") else: # Optional: Print error details for debugging print(f"Error: {buyres.status_code}") print(buyres.json())
Option 2: Use Requests' Built-in json Parameter (Simpler)
The requests library has a dedicated json parameter for POST requests that automatically serializes dictionaries to JSON and sets the correct Content-Type header (you can still keep your custom headers if needed). This is a cleaner approach than manual serialization:
Modified code:
import re from requests import post, get cookie = "blablabla" ID = 1562150 # getting x-csrf-token token = post("https://auth.roblox.com/v2/logout", cookies={".ROBLOSECURITY": cookie}).headers['X-CSRF-TOKEN'] print(token) # getting item details detail_res = get(f"https://www.roblox.com/library/{ID}") text = detail_res.text productId = int(get(f"https://api.roblox.com/marketplace/productinfo?assetId={ID}").json()["ProductId"]) expectedPrice = int(re.search("data-expected-price=\"(\d+)\"", text).group(1)) expectedSellerId = int(re.search("data-expected-seller-id=\"(\d+)\"", text).group(1)) headers = { "x-csrf-token": token } # Content-Type is set automatically by requests data = { "expectedCurrency": 1, "expectedPrice": expectedPrice, "expectedSellerId": expectedSellerId } # Use the json parameter instead of data buyres = post(f"https://economy.roblox.com/v1/purchases/products/{productId}", headers=headers, json=data, cookies={".ROBLOSECURITY": cookie}) if buyres.status_code == 200: print("Successfully bought item") else: # Optional: Print error details for debugging print(f"Error: {buyres.status_code}") print(buyres.json())
Additional Notes
- Make sure your
.ROBLOSECURITYcookie is valid and hasn't expired. - Double-check that the
expectedPriceandexpectedSellerIdvalues you're extracting match the actual item details (you can print these values to verify). - Adding error handling (like printing the response JSON on failure) will help you debug any future issues with the API.
内容的提问来源于stack exchange,提问作者Gray

