- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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:
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!
Solved! Go to Solution.
- Labels:
-
FortiSandbox
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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!
Manoj Papisetty
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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!
Manoj Papisetty
