HP ALM API : Attaching file to run step through ALM REST API not working - python-3.x

We have fetched Run Steps details using ALM REST API.
(for reference ALM API Documentation
Same question is being asked in ALM community, Community Microfocus ALM
https://some.almserver.com/qcbin/rest/domains/DOMAIN/projects/PROJECT/runs/<run-id>/run-steps
and passed Run Step id to below API and tried to attach file using POST and PUT.
https://some.almserver.com/qcbin/rest/domains/DOMAIN/projects/PROJECT/runs/<run-id>/run-steps/<run-step-ID>/attachments
we are getting 404 response.
Bad request.
URL = "https://some.almserver.com/qcbin/rest/domains/DOMAIN/projects/PROJECT/runs/<run-id>/run-steps/<step-id>/attachments"
METHOD = "PUT"
HEADERS = {
'cache-control': "no-cache",
"Slug": "some.html",
'Content-Type': "application/octet-stream"
}
def attach_to_run_step():
f = open("/path/to/some.html", "rb")
response = requests.put(URL, headers=HEADERS, cookies=cookies, data=f.read())
print(response.content)
Expected : 200 response
Actual : 404 status code

I think you need to use the following URL for your POST request:
https://some.almserver.com/qcbin/rest/domains/DOMAIN/projects/PROJECT/run-steps/<run-step-id>/attachments (remove /runs/<run-id>)
Update:
Here is an example, generated from Postman:
import requests
url = "http://xxx.xxx.xxx.xxx:8080/qcbin/rest/domains/DEFAULT/projects/demo/run-steps/1263/attachments"
payload="<file contents here>"
headers = {
'Content-Type': 'application/octet-stream',
'Slug': '1.jpg',
'Cookie': 'JSESSIONID=<session>; ALM_USER=2e69...; LWSSO_COOKIE_KEY=PTKYM...; QCSession=MTQwM...; XSRF-TOKEN=3dfa4...'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
P.S. We are developing commercial platform for integrating various test frameworks with ALM, which is called Agiletestware Bumblebee, so you might want to have a look at it.

Related

REST API Post method Recursive run returning Different data-probably data with error in Azure Functions

I am using REST API Post method and deployed to Azure functions .The First Run[immediate run post changes and deployment to azure] is success and subsequent runs are showing error in data and counts are not same as the first Run.Following is the code snippet that I am using.
` def __init__(self):
url = "https://login.microsoftonline.com/abcdefgh12345/oauth2/v2.0/token"
payload = 'grant_type=client_credentials&scope=api%AB1234567890CDE%.default'
headers = {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + "ABCDEFG",
'Cookie': 'XYZMANOP'
}
response = requests.request("POST", url, headers=headers, data=payload)
self.auth = response.json()`
Then I am using self.auth in other methods
Why is this happening and what is the solution???PS:When I try the code in local it works like a charm.
I tried extracting the access_token and added it in subsequent functions. But the result is also the same. Also I changed the schedule since access_token is valid only for 3600s and rescheduled to execute post 2 hours.But its returning same result

Get a SAS on a azure snapshot - Using Azure Compute API

I am trying to generate the SAS URI for one of the snapshot exist on resource group using microsoft provided API.
Below is the snippet code:
url = f"https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{target_resource_group}/providers/Microsoft.Compute/snapshots/{snap_name}/beginGetAccess"
headers = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'}
params = {'api-version': '2021-12-01'}
body= {
"access": "Read",
"durationInSeconds": 3000
}
json_body= json.dumps(body, indent=2)
accessSAS=requests.post(url, headers=headers, params=params, data=json_body, verify=False)
But I am receiving the response as <Response [202]>.
Could anyone help me with the issue.
Response 202 means that Azure accepts your request but it is handling your request. It is an asynchronous operation. Therefore, the response header will give you another url for getting the result. You can refer the following link to get what you want.
https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/async-operations

Google Oaut2 - Python using requests whiout Library

I am trying to authenticate not google by means of requests if I use the api but I don't know what I put in the "code" field and it will work, you can help me, or the code follows below.
import requests
url = "https://accounts.google.com/o/oauth2/token"
payload='grant_type=authorization_code&client_secret=xxxxx&client_id=xxxx&redirect_uri=https%3A%2F%2Fpushsistemas.com.br%2F&code='
headers = {
'content-type': 'application/x-www-form-urlencoded',
'Cookie': 'NID=219=RgUebbUM4k3WX0uQr8HVcIZkQ5-rMrb4HHnfnRzNTxUse776sG6-LItgXwpdv-MmaFYf3Tu4otJN-UfP70OzDtdnfam2dwAZkd6yMsr8BW_7lGE4FTaas9QkoG-hOMqXgYEHvI4YK39dWgXhWnL0E5LcsOv0DXnfjJ2kUf7VlTs; __Host-GAPS=1:jGXTa0ipDEC58oJF7pZwNBVeSXCQRw:3IRHy3GC9mAq4nqt'
}
response = requests.request("POST", url, headers=headers, data=payload)
This output
{
"error": "invalid_request",
"error_description": "Missing required parameter: code"
}
if it is not possible to do this way could you send me how to do it using the google library?

Rest API data reading present in swagger url using python library

I am trying to access swagger url that gives response upon sending request in json format. I have disabled to avoid SSLCertVerificationError using verify=False. How to access that please help me to read it using python code. You can get details what I have tried till now.
Installed requests library by using cmd in windows = pip install requests
I am able to get the 200 response by using code mentioned
When I am trying to use "params= payload" where. I am not able to get the response the same I am getting in the swagger url.
url : https:///#!/prepaid-estimate-controller/getAccountDetailsUsingPOST
payload={'searchCriteria': 'string', 'value': 'string'}
r4 = requests.get('https://<domain>/#!/prepaid-estimate-controller/getAccountDetailsUsingPOST', verify=False)
print(r4)
Output : 200
payload={'searchCriteria': 'string', 'value': 'string'}
r4 = requests.get('https://<domain>/#!/prepaid-estimate-controller/getAccountDetailsUsingPOST',params=payload, verify=False)
print(r4)
print(r4.text)
Output: DOM structure returns
This issue has been fixed by using below code generated in the postman.
import requests
url = "https:///bsi/prepaid/getAccountDetails"
payload = "{ \r\n "searchCriteria": "BAID", \r\n "value": "PPB006788"\r\n}"
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data = payload, verify=False)
print(response.text.encode('utf8'))

Error calling CF API login one time passcode

I am working with the CF API RESTful services. Trying to get an access token from cloud foundry's UAA API using https://login..../oauth/token web method.
I have verified that headers & body content is correct, but calling the api always returns a 400 error code with message missing grant type.
I have implemented this call in Objective-C, Swift & now Python. All tests return the same result. Here is my code example in Python:
import json
import requests
import urllib
params = {"grant_type": "password",
"passcode": "xxx"
}
url = "https://login.system.aws-usw02-pr.ice.predix.io/oauth/token"
headers = {"Authorization": "Basic Y2Y6", "Content-Type": "application/json", "Accept": "application/x-www-form-urlencoded"}
encodeParams = urllib.parse.urlencode(params)
response = requests.post(url, headers=headers, data=encodeParams)
rjson = response.json()
print(rjson)
Each time I run this, I get the response
error invalid request, Missing grant type
Any help would be greatly appreciated.
Your code mostly worked for me, although I used a different UAA server.
I had to make only one change. You had the Accept and Content-Type headers flipped around. Accept should be application/json because that's the format you want back, and Content-Type should be application/x-www-form-urlencoded because that's the format you are sending.
See the API Docs for reference.
import json
import requests
import urllib
import getpass
UAA_SERVER = "https://login.run.pivotal.io"
print("go to {}/passcode".format(UAA_SERVER))
params = {
"grant_type": "password",
"passcode": getpass.getpass(),
}
url = "https://login.run.pivotal.io/oauth/token"
headers = {
"Authorization": "Basic Y2Y6",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json"
}
encodeParams = urllib.parse.urlencode(params)
response = requests.post(url, headers=headers, data=encodeParams)
rjson = response.json()
print(json.dumps(rjson, indent=4, sort_keys=True))
I made a couple other minor changes, but they should affect the functionality.
Use getpass.getpass() to load the passcode.
Set the target server as a variable.
Pretty print the JSON response.
The only other thing to note, is that the OAuth2 client you use must be allowed to use the password grant type. It looks like you're using the same client that the cf cli uses, so if your UAA server is part of a standard Cloud Foundry install that is likely to be true, but if it still doesn't work for you then you may need to talk with an administrator and make sure the client is set up to allow this.

Resources