Databricks REST API

Muralijv
New Contributor

Hi , I am trying to create a Global Initscript using rest API as below successfully in the first step using Powershell. In the second step I am trying to enable it using rest api and getting the following error:  Any guidance or help is appreciated. 

STEP 1:

# Set variables
param([string]$databricksWorkspaceUrl, [string]$token, [string]$scriptName)

$databricksWorkspaceUrl = databricksWorkspaceUrl
$token = $token
$scriptName = $scriptName$scriptContent = Get-Content -Path "scriptfile.sh" -Raw
$scriptContent

$uri = "$databricksWorkspaceUrl/api/2.0/global-init-scripts"

$headers = @{
"Authorization" = "Bearer $token"
}

$base64Content = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($scriptContent ))

# Construct the request body JSON
$requestBody = @{
name = $scriptName
script = $base64Content
} | ConvertTo-Json

$requestBody

$response = Invoke-RestMethod -Uri $uri -Headers $headers -Method Post -Body $requestBody -ContentType "text/plain"

Write-Output $response

STEP 2: 

$keyToUpdate = "spark.databricks.init.script"
$newValue = "true"

# Define the REST API endpoint for updating workspace configuration
$apiEndpoint = "$databricksWorkspaceUrl/api/2.0/workspace-conf"

$headers = @{
"Authorization" = "Bearer $token"
}
# Construct the JSON payload for updating the configuration
$jsonPayload = @{
"key" = $keyToUpdate
"value" = $newValue
} | ConvertTo-Json
# Invoke the PATCH request to update the workspace configuration
$result = Invoke-RestMethod -Uri $apiEndpoint -Method POST -Headers $headers -Body $jsonPayload -ContentType "text/plain"
Write-Host $result
 
Step 2 is failing with the following error : 
$result = Invoke-RestMethod -Uri $apiEndpoint -Method POST -Headers …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Error 401 Unauthorized HTTP ERROR 401 Problem accessing /api/2.0/workspace-conf. Reason: Unauthorized

I have checked with the URL and Token and successful in connecting to the workspace from prompt.

 

feiyun0112
Honored Contributor

This  API use PATH method, but you use POST

PATCH /api/2.0/workspace-conf

https://docs.databricks.com/api/workspace/workspaceconf/setstatus