cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

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.

 

1 REPLY 1

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 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group