Support Forum
The Forums are a place to find answers on a range of Fortinet products from peers and product experts.
msc23
New Contributor II

submit / upload file via JSON API Call

after login and getting session id i will upload an 1,5 MB file with the following code:

 

def sendSmallFile(file_content_as_base64, file_name):
print("try to send small file to sandbox")
print(f"start of base64 content: {file_content_as_base64[:100]}")
print(f"got filename: {file_name}")
submitURL = "https://fortisand-api.our-domain.de/jsonrpc/"
submit_json_data = {
"method": "set",
"params": [
{
"file": file_content_as_base64,
"filename": file_name,
"skip_steps": "2",
"url": "/submit-file",
"type": "file",
"vrecord": "0",
"enable_ai": "",
"forcedvm": 1,
"browser": 0
}
],
"session": session_id,
"id": 11,
"ver": "4.4.0"
}

response = requests.post(submitURL, json=submit_json_data)
sid = getValueFromResponse(response.json(), "sid")
print(f"sid: {sid}")

 

the base64 content is encoded: 

file_content = file.read()
file_content_base64 = base64.b64encode(file_content)

 

in the debugger it looks ok:

 

debugger.png

 

but python get me an TypeError: Object of type bytes is not JSON serializable (in the code line to get the response from the request).

 

Any ideas?

 

thanks a lot!

 

 

 

 

1 Solution
mpapisetty
Staff
Staff

@msc23 ,

The issue is that base64.b64encode(file_content) will return an encoded output but as bytes. Bytes cannot be JSON serialized. You will have to use decode again to get it to parse via json. 

 

Example below for a sample content - 

 

 

>>> json.dumps(base64.b64encode(b'filecontent'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.10/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.10/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.10/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.10/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
>>>

 

 

The above error can be resolved by adding a decode() - 

 

 

>>> json.dumps(base64.b64encode(b'filecontent').decode())
'"ZmlsZWNvbnRlbnQ="'
>>>

 

 

Hope this helps!

 

-M

View solution in original post

1 REPLY 1
mpapisetty
Staff
Staff

@msc23 ,

The issue is that base64.b64encode(file_content) will return an encoded output but as bytes. Bytes cannot be JSON serialized. You will have to use decode again to get it to parse via json. 

 

Example below for a sample content - 

 

 

>>> json.dumps(base64.b64encode(b'filecontent'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.10/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.10/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.10/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.10/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
>>>

 

 

The above error can be resolved by adding a decode() - 

 

 

>>> json.dumps(base64.b64encode(b'filecontent').decode())
'"ZmlsZWNvbnRlbnQ="'
>>>

 

 

Hope this helps!

 

-M
Announcements

Select Forum Responses to become Knowledge Articles!

Select the “Nominate to Knowledge Base” button to recommend a forum post to become a knowledge article.

Labels
Top Kudoed Authors