0

I am able to perform a get request to retrieve SQL scripts within our workspace as mentioned in the docs: Sql Script - Get Sql Scripts By Workspace

%%pyspark
import requests
token = mssparkutils.credentials.getToken('Synapse')

# api-endpoint voor get requests
url = "https://xxxx-xx-xx-xxx-xx-xxx-xx.dev.azuresynapse.net/sqlScripts?api-version=2020-12-01"
headers = {'Authorization': f"Bearer {token}"}
response = requests.get(url,  headers=headers)

However, I also want to be able to create new sql script using the same method, but instead performing a PUT request as mentioned here: Sql Script - Create Or Update Sql Script

#instead of request.get now using put
requests.put(url2, data=data, headers=headers2)

I am getting a '202 Accepted' response, meaning the response was accepted. Unfortunately no new scripts are created in our workspace. The response state does say "state":"Creating","created"...

What am I doing wrong?

This is an example of the response output:

b'{"id":"/subscriptions/xxxx-xx-xxx-xxx-xxxx/resourceGroups/xxx-xx-xxx-xxx-xx/providers/Microsoft.Synapse/workspaces/xxx-xx-xx-xxx-xx-xxxx-xxx/sqlScripts/test","recordId":xxxx,"state":"Creating","created":"2023-10-12T15:40:03.6666667Z","changed":"2023-10-12T15:40:03.6666667Z","type":"SqlScript","name":"test","operationId":"xxxxxx-xxx-xxx-xxxx-xxxxxx","artifactId":"xxxxx-xxxxx-xxxx-xxx-xxxxxxxx"}' <Response [202]>

1 Answer 1

1

You need to pass the json body with name and properties with following details.

request_body = {
  "name": "user",
  "properties":
    {'content':{'query': "SELECT TOP 100 * FROM user;",
                'metadata': {'language': 'sql'},
                'currentConnection': {'databaseName': 'master', 'poolName': 'Built-in'},
                'resultLimit': 5000},
      'type': 'SqlQuery'}
  }

Here, i am creating the script called user.

u = "<endpoint>t/sqlScripts/user?api-version=2020-12-01"
token = mssparkutils.credentials.getToken('Synapse')
headers = {
    'Authorization': f"Bearer {token}",
    "Content-Type": "application/json"
}
request_body = {
  "name": "user",
  "properties":
    {'content':{'query': "SELECT TOP 100 * FROM user;",
                'metadata': {'language': 'sql'},
                'currentConnection': {'databaseName': 'master', 'poolName': 'Built-in'},
                'resultLimit': 5000},
      'type': 'SqlQuery'}
  }

r = requests.put(u,headers=headers,json=request_body)
print(r.status_code)
print(r.content)

enter image description here

After hitting on refresh button, the script is loaded.

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I've passed the json body with the exact same details and I get the exact same response. Unfortunately the created scripts do not appear within my workspace, also not after a refresh. I have no clue where or how I can still access or view them. Very strange.. it is as if I do not have the right credentials, or that the scripts are created on a different workspace....
what is the state you got? is it creating? If yes then script is created. make sure you are using correct endpoint.
Yes, I get "state":"Creating". So the script is created somewhere... I am using the same endpoint by which I am able to get request the sql scripts. So it is extremely confusing that I am unable to find the newly created scripts.
Sry my mistake. I've uploaded the code. Need to give request body to json parameter.
Give json=request_body in request.put and do refresh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.