You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roblox商城资产购买脚本出现500内部服务器错误,如何添加json.dumps()并修复问题

Fixing Roblox Asset Purchase Script (500 Internal Server Error)

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 .ROBLOSECURITY cookie is valid and hasn't expired.
  • Double-check that the expectedPrice and expectedSellerId values 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 21:18:12