Roblox API: Internal server error (Status code 500) while trying to create a badge

Title explains it all. Been trying to figure this out all day, I just don’t understand why it isn’t working.
The badges api is the only one I am having issues with and I can confirm the account’s cookie is valid. The code was working before but just stopped working not too long ago.

Python code to create a badge:

def createBadge(self, id, name, description, image):
        data = {
            "name": "name",
            "description": "description",
            "paymentSourceType": "User",
            "expectedCost": 0
        }
        files = {'upload_file': open("badgeimages/" + image,"rb")}
        req = self.session.post(f"https://badges.roblox.com/v1/universes/{id}/badges", data=data, files=files)
        return req["id"]

self.session is a custom class of robloxsession:

def request(self, method, url, **kwargs):
        json_value = kwargs.pop('decode_json', True)
        while 1:
            req = self.baseSession.request(method, url, **kwargs) #basesession is just a normal python requests session.
            if "X-CSRF-TOKEN" in req.headers:
                self.baseSession.headers["X-CSRF-TOKEN"] = req.headers["X-CSRF-TOKEN"]
                
            if req.status_code == 200:
                if json_value:
                    return json.loads(req.text)
                else:
                    return req.text
            else:
                print(url)
                self.handleResponseCode(req) # handle response code just prints out an error message
    
    def post(self, url, **kwargs):
        return self.request('post', url, **kwargs)

Request Headers: {'User-Agent': 'python-requests/2.28.1', 'Accept-Encoding': 'gzip, deflate', 'accept': 'application/json', 'Connection': 'keep-alive', 'X-CSRF-TOKEN': 'CSRF PLACEHOLDER', 'Cookie': '.ROBLOSECURITY=COOKIE PLACEHOLDER; rbx-ip2=', 'Content-Length': '550', 'Content-Type': 'multipart/form-data; boundary=bb8517293e3e961b2e7dfbd4e303fe56'}
(Cookie and CSRF placeholders are intentional. Don’t want to leak my account cookie.)

Request Body: b'--42fdbee1e9d775f1239dda28331699a7\r\nContent-Disposition: form-data; name="name"\r\n\r\nname\r\n--42fdbee1e9d775f1239dda28331699a7\r\nContent-Disposition: form-data; name="description"\r\n\r\ndescription\r\n--42fdbee1e9d775f1239dda28331699a7\r\nContent-Disposition: form-data; name="paymentSourceType"\r\n\r\nUser\r\n--42fdbee1e9d775f1239dda28331699a7\r\nContent-Disposition: form-data; name="expectedCost"\r\n\r\n0\r\n--42fdbee1e9d775f1239dda28331699a7\r\nContent-Disposition: form-data; name="upload_file"; filename="normal.png"\r\n\r\n\r\n--42fdbee1e9d775f1239dda28331699a7--\r\n'

Response Headers: {'content-length': '55', 'content-type': 'application/json', 'date': 'Wed, 31 Jan 2024 00:06:26 GMT', 'server': 'Kestrel', 'cache-control': 'no-cache', 'strict-transport-security': 'max-age=3600', 'x-frame-options': 'SAMEORIGIN', 'roblox-machine-id': '23130f3bceda', 'x-roblox-region': 'us-central', 'x-roblox-edge': 'atl1', 'report-to': '{"group":"network-errors","max_age":604800,"endpoints":[{"url":"https://ncs.roblox.com/upload"}]}', 'nel': '{"report_to":"network-errors","max_age":604800,"success_fraction":0.001,"failure_fraction":1}'}

Response Body: {"errors":[{"code":0,"message":"InternalServerError"}]}

To confirm:
Cookie is valid
Gameid is valid (Universe id)
I can create badges from the creator dashboard
All other api requests work

1 Like

turns out the problem was the file was getting closed before the request was sent, leading to the file content being epty and the server erroring. All I had to do was something like this: store the file content in a variable, close the file, then send the request.

with open(fp, 'rb') as f:
    content = f.read()
    f.close()

here’s the final code:

def createBadge(self, id, name, description, image):
    data = {
        'name': name,
        'description': description,
        'paymentSourceType': 'User',
        'expectedCost': 0,
    }
    files = {'files': read_file_content('badgeimages/' + image)}
    req = self.session.post(f"http://badges.roblox.com/v1/universes/{id}/badges", data=data, files=files)
    return req["id"]

def read_file_content(fp):
    with open(fp, 'rb') as f:
        content = f.read()
        f.close()
    return content

Hope this helps someone in the future. Don’t want anyone experiencing the same amount of pain I did.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.