I am attempting to copy a notebook within my Databricks workspace using the Databricks REST API. I successfully export the notebook using the export API but encounter a problem when trying to import it into another folder.
Here’s how I’m exporting the notebook:
<code>headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
# Data to export the notebook
export_data = {
'path': source_path,
'direct_download': True
}
# Execute the GET request to export the notebook
export_response = requests.get(f'https://{host}/api/2.0/workspace/export', headers=headers, params=export_data)
</code>
<code>headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
# Data to export the notebook
export_data = {
'path': source_path,
'direct_download': True
}
# Execute the GET request to export the notebook
export_response = requests.get(f'https://{host}/api/2.0/workspace/export', headers=headers, params=export_data)
</code>
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
# Data to export the notebook
export_data = {
'path': source_path,
'direct_download': True
}
# Execute the GET request to export the notebook
export_response = requests.get(f'https://{host}/api/2.0/workspace/export', headers=headers, params=export_data)
And it seems to work fine:
<code>export_response
<Response [200]>
export_response.text
'{"content":"IyBEYXRhYnJpY2tzIG5vdGVib29rIHNvdXJjZQojIFNob3cvaGlkZSBub3RlYm9vayBjZWxscwpmcm9tIElQeXRob24uZGlzcGxheSBpbXBvcnQgSFRNTCwgSmF2YXNjcmlwdCwgZGlzcGxheSwgY2xlYXJfb3V0cHV0CgpIVE1MKAogICAgJycnCiAgICAgICAgPHNjcmlwdD4KICAgICAgICAgICAgZnVuY3Rpb24gY29kZV90b2dnbGUoKXsKICAgICAgICAgICAgICAgIGlmIChjb2RlX3Nob3duKXsKICAgICAgICAgICAgICAgICAgICAkKCdkaXYuaW5...
</code>
<code>export_response
<Response [200]>
export_response.text
'{"content":"IyBEYXRhYnJpY2tzIG5vdGVib29rIHNvdXJjZQojIFNob3cvaGlkZSBub3RlYm9vayBjZWxscwpmcm9tIElQeXRob24uZGlzcGxheSBpbXBvcnQgSFRNTCwgSmF2YXNjcmlwdCwgZGlzcGxheSwgY2xlYXJfb3V0cHV0CgpIVE1MKAogICAgJycnCiAgICAgICAgPHNjcmlwdD4KICAgICAgICAgICAgZnVuY3Rpb24gY29kZV90b2dnbGUoKXsKICAgICAgICAgICAgICAgIGlmIChjb2RlX3Nob3duKXsKICAgICAgICAgICAgICAgICAgICAkKCdkaXYuaW5...
</code>
export_response
<Response [200]>
export_response.text
'{"content":"IyBEYXRhYnJpY2tzIG5vdGVib29rIHNvdXJjZQojIFNob3cvaGlkZSBub3RlYm9vayBjZWxscwpmcm9tIElQeXRob24uZGlzcGxheSBpbXBvcnQgSFRNTCwgSmF2YXNjcmlwdCwgZGlzcGxheSwgY2xlYXJfb3V0cHV0CgpIVE1MKAogICAgJycnCiAgICAgICAgPHNjcmlwdD4KICAgICAgICAgICAgZnVuY3Rpb24gY29kZV90b2dnbGUoKXsKICAgICAgICAgICAgICAgIGlmIChjb2RlX3Nob3duKXsKICAgICAgICAgICAgICAgICAgICAkKCdkaXYuaW5...
Then I try to paste the notebook in the destination folder with the import api call:
<code>import_data = {
'content': export_response.content,
'path': dest_path
}
import_response = requests.post(f'https://{host}/api/2.0/workspace/import', headers=headers, json=import_data)
</code>
<code>import_data = {
'content': export_response.content,
'path': dest_path
}
import_response = requests.post(f'https://{host}/api/2.0/workspace/import', headers=headers, json=import_data)
</code>
import_data = {
'content': export_response.content,
'path': dest_path
}
import_response = requests.post(f'https://{host}/api/2.0/workspace/import', headers=headers, json=import_data)
And I get:
<code>import_response
<Response [400]>
import_response.text
'{"error_code":"MALFORMED_REQUEST","message":"Could not parse request object: Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '{' (code 0x7b) in base64 contentn at [Source: (ByteArrayInputStream); line: 1, column: 14]n at [Source: java.io.ByteArrayInputStream@3fbdb5c3; line: 1, column: 14]"}n'
</code>
<code>import_response
<Response [400]>
import_response.text
'{"error_code":"MALFORMED_REQUEST","message":"Could not parse request object: Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '{' (code 0x7b) in base64 contentn at [Source: (ByteArrayInputStream); line: 1, column: 14]n at [Source: java.io.ByteArrayInputStream@3fbdb5c3; line: 1, column: 14]"}n'
</code>
import_response
<Response [400]>
import_response.text
'{"error_code":"MALFORMED_REQUEST","message":"Could not parse request object: Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '{' (code 0x7b) in base64 contentn at [Source: (ByteArrayInputStream); line: 1, column: 14]n at [Source: java.io.ByteArrayInputStream@3fbdb5c3; line: 1, column: 14]"}n'
I’m stuck I don’t understand would should I put as a correct content, can you please assist me?
Thanks