Is there a way to remove the default headers from the uploaded file when we post a file using a PUT request - python-3.x

I have a simple put request for to upload a file to an API using a post request.
api_key = "Auth token"
request_headers = {
"Authorization": "Bearer {}".format(api_key),
"Accept": "*/*",
"Content-Type": "text/plain",
}
url = "https://graph.microsoft.com/v1.0/sites/vmt8c.sharepoint.com,01f460f4-3c8a-470d-ace6-4058b2c78952,3589d1c5-e699-45a0-b8ab-1027175c367f/drive/items/root:/test-folder/aaa/test2.txt:/content"
files = {"my_file": open("params.yml", "rb")}
res = requests.put(url, files=files, headers=request_headers)
params.yml contains:
Hello! This is a test.
The uploaded data I get in /test-folder/aaa/test2.txt :
--222e00b3de068a0895a4f0f4bbd8a992
Content-Disposition: form-data; name="my_file"; filename="my_file"
Hello! This is a test
--222e00b3de068a0895a4f0f4bbd8a992--
I would like to get only "Hello! This is a test" in the uploaded data and remove the headers and everything else.
I tried a few approaches but couldn't succeed. Please help on this.

Related

HP ALM API : Attaching file to run step through ALM REST API not working

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.

Upload attachments using clickup api

I'm trying to upload the attachments to a task on clickup.
Clickup API
Click up does provide example code the below is clickup example code
from urllib2 import Request, urlopen
values = """
attachment: raw_image_file (file)
filename: example.png (string)"""
headers = {
'Authorization': '\'access_token\'',
'Content-Type': 'multipart/form-data'
}
request = Request('https://private-anon-f799579c66-clickup20.apiary-mock.com/api/v2/task/{task_id}/attachment?custom_task_ids=&team_id=
', data=values, headers=headers)
response_body = urlopen(request).read()
print response_body
I used it as reference and used requests lib for the project
Here's the code using request lib
import requests
attachment_headers = {'Authorization': self.access_token, 'Content-Type': 'multipart/form-data'}
r = requests.post(f"https://private-anon-df9b125a00-clickup20.apiary-mock.com/api/v2/task/{task_id}/attachment",
files={"attachment": ("attachment", open("attachment.png", "rb")), "filename": "example.png"},
headers=attachment_headers)
print(r)
print(r.json())
I do get the status code as 200 and no error message but when I check on clickup task it doesn't show any attachments
Thanks for the help in advance!
You must remove the 'filename' part of your files param.
file = {"attachment": ('choose_your_name.png', open('file.png', 'rb'))}
headers = {'Authorization': config.clickup_access_token}
request = requests.post(f'https://api.clickup.com/api/v2/task/{task_id}/attachment', files=file, headers=headers)
print(request.json())

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?

Python - Basic Authorization (OAuth2.0) to retrieve access token from API

I'm trying to generate an access token from an API using the following required information:
Authorization endpoint: https://api.paylocity.com/IdentityServer/connect/token
Authorization Header
The request is expected to be in the form of a basic authentication request, with the "Authorization" header containing the client-id and client-secret. This means the standard base-64 encoded user:password, prefixed with "Basic" as the value for the Authorization header, where user is the client-id and password is the client-secret.
Content-Type Header
The "Content-Type" header is required to be "application/x-www-form-urlencoded".
Other Values
The request must post the following form encoded values within the request body:
grant_type = client_credentials
scope = WebLinkAPI
I've tried using the Python 'requests' package without success, see below:
import requests
url = "https://api.paylocity.com/IdentityServer/connect/token"
headers = {
'Authorization': "Basic **********:**********",
'Content-Type': "application/x-www-form/urlencoded"
}
body = {
'grant_type': "client_credentials",
'scope': 'WebLinkAPI'
}
response = requests.request("POST", url, headers=headers, params=body)
print(response.text)
Instead of an access token, I receive an error message:
{"error":"invalid_client"}
I've verified that my base64 encoded username and password are correct, it looks something like "G/RaNdOm:MoReRaNdOm==" and when I add them to postman it works so my credentials are correct. What could be wrong?
I resolved this. There were two issues, the Authorization header was not using ASCII as the destination charset with base64 encoding. The other issue was trying to add the additional values to the parameters instead of the body in the requests package. The solution was to replace "params" with "data". Here was the solution:
url = "https://api.paylocity.com/IdentityServer/connect/token"
body = "grant_type=client_credentials&scope=WebLinkAPI"
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'Authorization': "Basic ***(base64ASCII)username:password***",
}
response = requests.request("POST", url, data=body, headers=headers)
print(response.text)

Post request error: Missing or Invalid Post Body

I'm getting the following error and I'm not sure why:
{"success":false,"errors":["Missing or invalid POST body."],"results":[]}
I'm not sure if there is a problem with how I have my code structured, sending bad json, or what. If you could give me an idea of where to troubleshoot this, that would be great. Thanks. Here is how I have my settings:
headers = {
"Accept": "application/json",
"Authorization": "bearer " + bearer_token,
"Content-Type": "application/json",
}
data = {
'limit':10,
'sort':"MinPrice DESC",
'filters':[
{
'name':'SetName',
'values':'All'
}
]
}
url = 'http://api.tcgplayer.com/V1.9.0/catalog/categories/3/search'
def catalog_data():
r = requests.post(url, headers=headers, data=data)
print(r.text)
catalog_data()
Making the change from data=data to json=data allowed my Post data to be read properly.

Resources