I'm trying to send a request to a server with some JSON, but I keep getting a TypeError about it not being serialized. I tried json=data and using headers, but it keeps throwing that error.
Could this be the variables inside the request, or something else?
Short Error:
TypeError: Object of type set is not JSON serializable
Traceback:
Ignoring exception in command limiteds:
Traceback (most recent call last):
File "C:\Users\foo\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\foo\Documents\Programs\Python\foo.py", line 471, in limiteds
send_trade = requests.post(send_trade_api,data=json.dumps(req), headers=headers)
File "C:\Users\foo\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Users\foo\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\foo\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\foo\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type set is not JSON serializable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\foo\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\foo\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\foo\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
**discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: Object of type set is not JSON serializable**
Full Code:
req = {
"offers": [
{
"userId": {our_userid},
"userAssetIds": [
{our_userAssetId}
],
"robux": 0
},
{
"userId": {userid},
"userAssetIds": [
{userAssetId}
],
"robux": 0
}
]}
headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
send_trade_api = 'https://trades.roblox.com/v1/trades/send'
send_trade = requests.post(send_trade_api,data=json.dumps(req), headers=headers)
CodePudding user response:
This:
"userAssetIds": [
{our_userAssetId}
],
And this:
"userAssetIds": [
{userAssetId}
],
Are sets in lists. To avoid that you should just write the code this way:
"userAssetIds": [
our_userAssetId,
],
The sets are predefined by similar syntax like dicts, but there are no :
separators, so set is defined for example by using syntaxes like:
{1}
...
{1, 2, 3}
Also you need to change your userId
key value too.
CodePudding user response:
You better use list instead of set when converting to json, but if your really need to use set should deal without third-party modules (I think there are still some sophistications to do this manually).
But the reciever will still get list.
Just check this instruction: https://appdividend.com/2022/06/23/python-set-to-json/