jupyter-notebookbase64azure-databricks

Databricks API Error When Importing Notebook: "MALFORMED_REQUEST" with Base64 Encoding Issue


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:

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:

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:

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:

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 content\n at [Source: (ByteArrayInputStream); line: 1, column: 14]\n at [Source: java.io.ByteArrayInputStream@3fbdb5c3; line: 1, column: 14]"}\n'

I also tried to encode the content this way:

import_data = {
    'content': base64.b64encode(export_response.text.encode()),
    'path': dest_path
}

But I still receive:

<Response [400]>
'{"error_code":"INVALID_PARAMETER_VALUE","message":"The zip file may not be valid or may be an unsupported version."}\n'

I'm stuck I don't understand would should I put as a correct content, can you please assist me? Thanks


Solution

  • The response from export Api is a binary base 64 encoded string having content and file type.

    enter image description here

    For import Api requires only the content, so get the content and pass it to import Api along with the language you use.

    import_data = {
    'content': export_response.json()['content'],
    'path': "/Users/xxxxxxyyyyy/tst",
    'language':'PYTHON'
    }
    
    import_response = requests.post(f'https://{host}/api/2.0/workspace/import', headers=headers, json=import_data)
    

    Output:

    enter image description here