Skip to main content
msc23
Explorer
May 15, 2024
Solved

submit / upload file via JSON API Call

  • May 15, 2024
  • 1 reply
  • 1770 views

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!

 

 

 

 

Best answer by mpapisetty

@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!

 

1 reply

mpapisetty
Staff
Staff
May 16, 2024

@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!

 

Thought Leadership Security Summit. Outpace New Threats with AI - enhanced defense. Tuesday, Septmeber 15, 8:30 AM - 2:30 PM PT. The Golf Club at Newcastle, WA.
Fortinet Flag the Hack. Wednesday, August 26, 9:00 AM - 5:00 PM ET, COSM, Atlanta, GA.
Virtual event | September 2026. SASE summit. The age of autonomous trust. Register here!