Item does not have a file (error 500) when uploading a PNG file to a portal using add item method - rest

I am trying to setup a POST request method using the "Add Item" operation within a REST API for my portal so that the PNG images can be add and later updated. Currently my script uploads the item to the portal but when i try to download the file or share it then it opens a page saying "Item does not have a file . Error 500" I assume its something to do with how i am building the POST request. How should i send the file over the POST request so that i can later download and update the file . Here is my current code:
def add_item(username,files,type,title):
"""
Add an item to the portal
Input:
- file: path of the the file to be uploaded
- username: username of user uploads the item
Return:
- Flag with a list of messages
"""
# set request header authorization type
header = {
'Authorization': f'Bearer {token}'
}
# build url
u_rl = f"{portal}/sharing/rest/content/users/{username}/addItem"
# search for id
req = requests.post(url=u_rl, headers=header,files=files,data={ "type":type,"title":title,"f": "json"})
response = json.loads(req.text)
if 'error' in response.keys():
error = response["error"]
raise Exception(
f"Error message: {error['message']}", f"More details: {','.join(error['details'])}")
return response["success"] if 'success' in response.keys() else False
if __name__ == "__main__":
user = input("input your USERNAME: ")
password = getpass.getpass("input your PASSWORD: ")
portal = "https://cvc.portal"
token = generate_token(user, password, portal=portal)
files = {'upload_file': open(r'C:\Users\test.png','rb')}
type='Image',
title='An image'
add_item(user,files,type,title

Related

Databricks REST API call for updating branch error : User Settings > Git Integration to set up an Azure DevOps personal access token

I am getting below error for updating the repo to a different branch using databricks rest api as mentioned at https://docs.databricks.com/dev-tools/api/latest/repos.html#operation/update-repo .
I have authenticated using service principal and generated dbrks_bearer_token and dbrks_management_token.
Please find below code for same :-
import requests
import os
import json
TOKEN_REQ_BODY = {
'grant_type': 'client_credentials',
'client_id': 'client_id',
'client_secret': 'client_secret'
}
TOKEN_BASE_URL = 'https://login.microsoftonline.com/' + 'tenant_id' + '/oauth2/token'
TOKEN_REQ_HEADERS = {'Content-Type': 'application/x-www-form-urlencoded'}
def dbrks_management_token():
TOKEN_REQ_BODY['resource'] = 'https://management.core.windows.net/'
response = requests.get(TOKEN_BASE_URL, headers=TOKEN_REQ_HEADERS, data=TOKEN_REQ_BODY)
if response.status_code == 200:
print(response.status_code)
return response.json()['access_token']
else:
raise Exception(response.text)
return response.json()['access_token']
def dbrks_bearer_token():
TOKEN_REQ_BODY['resource'] = '2ff814a6-3304-4ab8-85cb-cd0e6f879c1d'
response = requests.get(TOKEN_BASE_URL, headers=TOKEN_REQ_HEADERS, data=TOKEN_REQ_BODY)
if response.status_code == 200:
print(response.status_code)
else:
raise Exception(response.text)
return response.json()['access_token']
DBRKS_BEARER_TOKEN = dbrks_bearer_token()
DBRKS_MANAGEMENT_TOKEN = dbrks_management_token()
DBRKS_REQ_HEADERS = {
'Authorization': 'Bearer ' + DBRKS_BEARER_TOKEN,
'X-Databricks-Azure-Workspace-Resource-Id':
'/subscriptions/susbcriptionid' +
'/resourceGroups/rg-dev/providers/Microsoft.Databricks/workspaces/dbr-dev',
'X-Databricks-Azure-SP-Management-Token': DBRKS_MANAGEMENT_TOKEN }
DBRKS_CLUSTER_ID = {'cluster_id': 'cluster_id'}
def update_repo():
DBRKS_START_ENDPOINT = 'api/2.0/repos/0328767704612345'
postjson = """{
"branch": "main"
}"""
response = requests.patch("https://adb-1234582271731234.0.azuredatabricks.net/"
+ DBRKS_START_ENDPOINT,
headers=DBRKS_REQ_HEADERS,
json=json.loads(postjson))
print(response.status_code)
if response.status_code != 200:
raise Exception(response.text)
os.environ["DBRKS_CLUSTER_ID"] = response.json()["cluster_id"]
print(response.content)
update_repo()
I am getting below error:-
Traceback (most recent call last):
File "C:/Users/IdeaProjects/DBCluster/DB_rest_api.py", line 109, in <module>
update_repo()
File "C:/Users/IdeaProjects/DBCluster/DB_rest_api.py", line 104, in update_repo
raise Exception(response.text)
Exception: {"error_code":"PERMISSION_DENIED","message":"Missing Git provider credentials. Go to User Settings > Git Integration to add your personal access token."}
403
Can someone please let me know if i need to anything explicitly at azure devops git configuration level as well?
Many thanks..!!
To do any operations with Databricks Repos, you need to set a Git personal access token. Because you're using service principal, you can't set that personal access token via UI, but you can perform setting of that Git token using recently implemented Git Credentials REST API - just do that once for your service principal (or when Git PAT expires), and then Repos operations will work.

SoapUI POST to REST with attached file in Groovy

I'm trying to POST to a Sharepoint REST service an attached file with SoapUI Pro. I've tried the examples at: https://support.smartbear.com/readyapi/docs/requests/attachment/rest.html
But with no luck.
It should work with POST with byte-array as body. But how do I do that in SoapUI and Groovy?
In the tool Insomnia it works with "Binary File".
I add these headers:
Accept: application/json;odata=verbose
Content-Type: application/octet-stream
Media type = multipart/mixed and Post QueryString
But the file won't be uploaded to SharePoint.
PowerShell code that works:
$headers = #{
'X-RequestDigest' = 'xxxxxxxxxxxxxxxxxxxxxxx'
'Accept' = 'application/json;odata=verbose'
}
$document = [System.IO.File]::ReadAllBytes('C:\temp\myFile.docx')
Invoke-RestMethod -Method Post -UseDefaultCredentials -Uri "https://xxxx.xxx/add(url='myFile.docx',%20overwrite=true)" -Headers $headers -Body $document
I tried to go through this as well a while ago but I found it easier to use HTTP to do this.
You may try to see if it fits your requirements
My groovy script for attachment :
// upload source file before import
// get uploading request
def source_file = context.expand( '${#TestCase#source_file_path}' )
log.info "upload $source_file"
def aPIToolsTestSuite = context.expand( '${#Project#APIToolsTestSuite}' ) // the test suite that contains the test case with the HTTP request
tc_name = "import - upload resource files"
request = testRunner.testCase.testSuite.project.testSuites[aPIToolsTestSuite].testCases[tc_name].getTestStepByName("Request 1").testRequest
// clear request from any existing attachment
for (a in request.attachments)
{
request.removeAttachment(a)
}
// attach file to upload
def file = new File(source_file)
if (file == null)
{
log.error "bad file name : $source_file"
}
else
{
// attach file and set properties
try{
def attachment = request.attachFile (file, true)
attachment.contentType = "application/octet-stream"
attachment.setPart("upload file '$source_file'")
}
catch (Exception e){
log.error "file ${file.name} : exception $e"
}
}
// now upload file - launch the request
def jsonSlurper = new groovy.json.JsonSlurper()
def TC;
def async = false
TC = testRunner.testCase.testSuite.project.getTestSuiteByName(aPIToolsTestSuite).getTestCaseByName(tc_name)
result = TC.run (context.getProperties(), async)
if (String.valueOf( result.status ) != "PASS")
{
msg = "unexpected failure during $tc_name when uploading $source_file"
testRunner.fail(msg)
log.error msg
}
else
{
// this part is for further processing
// file uploaded, go through the import and properties backup process
resource_to_import = TC.getPropertyValue("testResponse").split('\"')[1]
// file uploaded, go through the import and properties backup process
testRunner.testCase.setPropertyValue("resource_id", resource_to_import)
}
And the HTTP request contained in the test case APIToolsTestSuite/import - upload resource files
first step : get endpoint
def env = testRunner.testCase.testSuite.project.activeEnvironment
rest = env.getRestServiceAt(0)
config = rest.getEndpoint().config
endpoint = new XmlSlurper().parseText(config.toString())
testRunner.testCase.setPropertyValue("endpoint", endpoint.toString())
second step, HTTP request:
POST
with Request tab parameters :
name : metadata
value : {"storageType":"FILESYSTEM","itemName":"my_source_file"}
type : QUERY
media type : multipart/form-data
Post QueryString
Headers : application/json
Good luck :)

How to fetch collection of Zuora Accounts using REST API

I want to fetch all customer accounts from Zuora. Apart from Exports REST API, Is there any API available to fetch all accounts in a paginated list?
This is the format I used to fetch revenue invoices, use this code and change the endpoint
import pandas as pd
# Set the sleep time to 10 seconds
sleep = 10
# Zuora OAUTH token URL
token_url = "https://rest.apisandbox.zuora.com/oauth/token"
# URL for the DataQuery
query_url = "https://rest.apisandbox.zuora.com/query/jobs"
# OAUTH client_id & client_secret
client_id = 'your client id'
client_secret = 'your client secret'
# Set the grant type to client credential
token_data = {'grant_type': 'client_credentials'}
# Send the POST request for the OAUTH token
access_token_resp = requests.post(token_url, data=token_data,
auth=(client_id, client_secret))
# Print the OAUTH token respose text
#print access_token_resp.text
# Parse the tokens as json data from the repsonse
tokens = access_token_resp.json()
#print "access token: " + tokens['access_token']
# Use the access token in future API calls & Add to the headers
query_job_headers = {'Content-Type':'application/json',
'Authorization': 'Bearer ' + tokens['access_token']}
# JSON Data for our DataQuery
json_data = {
"query": "select * from revenuescheduleiteminvoiceitem",
"outputFormat": "JSON",
"compression": "NONE",
"retries": 3,
"output": {
"target": "s3"
}
}
# Parse the JSON output
data = json.dumps(json_data)
# Send the POST request for the dataquery
query_job_resp = requests.post(query_url, data=data,
headers=query_job_headers)
# Print the respose text
#print query_job_resp.text
# Check the Job Status
# 1) Parse the Query Job Response JSON data
query_job = query_job_resp.json()
# 2) Create the Job URL with the id from the response
query_job_url = query_url+'/'+query_job["data"]["id"]
# 3) Send the GETrequest to check on the status of the query
query_status_resp = requests.get(query_job_url, headers = query_job_headers)
#print query_status_resp.text
# Parse the status from teh response
query_status = query_status_resp.json()["data"]["queryStatus"]
#print ('query status:'+query_status)
# Loop until the status == completed
# Exit if there is an error
while (query_status != 'completed'):
time.sleep(sleep)
query_status_resp = requests.get(query_job_url, headers = query_job_headers)
#print query_status_resp.text
query_status = query_status_resp.json()["data"]["queryStatus"]
if (query_status == 'failed'):
print ("query: "+query_status_resp.json()["data"]["query"]+' Failed!\n')
exit(1)
# Query Job has completed
#print ('query status:'+query_status)
# Get the File URL
file_url = query_status_resp.json()["data"]["dataFile"]
print (file_url)```
If you don't want to use Data Query or any queue-based solution like that, use Zoql instead.
Note! You need to know all fields from the Account object you need, the asterisk (select *) doesn't work here:
select Id, ParentId, AccountNumber, Name from Account
You may also add custom fields into your selection. You will get up to 200 records per page.

File upload Error : FlaskRestful - Getting 400 for POST request

I am trying to upload a file through the post request in flask restfulplus
I followed the example in this link
Here is my code
#ns.route('/upload/logo/')
#ns.doc('Upload Logo')
class UploadImage(Resource):
#ns.param('picture', 'image file path')
# #ns.route('/upload/')
def post(self):
parse = reqparse.RequestParser()
parse.add_argument('picture', type=FileStorage, location='files', required=True)
argsd = parse.parse_args()
print(argsd)
imgFile = argsd['picture']
print(imgFile)
imgFile.save("your_file_name.jpg")
return {'successfully' : 'loaded'}
For some reason the file is not getting uploaded. Can someone help me to solve this problem.
[Input Upload][2]
[2]: https://i.stack.imgur.com/BQc4W.png

Grunt task for uploading a file to CouchDB using request

In my Gruntfile.coffee, I have set up a task:
grunt.registerTask 'couch_upload', 'CouchDB upload', ->
done = #async()
assets = ['combined.min.js', 'screen.min.css']
ddoc = 'http://localhost:5984/mydb/_design/app'
request = require 'request'
fs = require 'fs'
grunt.log.writeln 'Uploading assets...'
upload = (asset) ->
# get the revision of the design document
request.get {json: yes, url: ddoc}, (err, resp, body) ->
rev = body._rev
# stream the file and pipe it, so it has the correct Content-Type
fs.createReadStream('build/' + asset).pipe request.put
auth: user: 'myusername', password: 'mypass'
headers: 'If-Match': rev
url: [ddoc, asset].join '/'
, -> grunt.log.write ' ' + asset
upload asset for asset in assets
done()
but I cannot see anything uploaded, neither on Grunt's log, nor Couch's log.
Can you tell if I'm doing something wrong?
Sounds like you need to open a feature request to CouchDB-Lucene: https://github.com/rnewson/couchdb-lucene/issues — it doesn’t look like it has that feature enabled yet, if it is at all possible.