Discussion
Attachment Upload via URL for Targetprocess API
about 1 month ago by null
I am in the process of developing an integration that involves programmatically uploading attachments to a bug ticket within Targetprocess using the API. I have been utilizing the UploadFile.ashx endpoint for this task.
My current approach involves fetching the file content from a URL and then uploading it as an attachment to a specific bug ID using a POST request with multipart/form-data.
Could you please advise on the following points:
- Does the UploadFile.ashx endpoint support this method of uploading files using content fetched from a URL?
- Are there any known issues or limitations with the UploadFile.ashx endpoint that might result in empty file contents upon upload?
- Can you confirm whether there are any additional headers or metadata that need to be included in the POST request to ensure the file content is uploaded correctly?
- Here is a part of the code I use to upload :
def upload_attachment_to_targetprocess(bug_id, attachment_url):
targetprocess_domain = os.environ['TARGETPROCESS_DOMAIN']
targetprocess_access_token = os.environ['TARGETPROCESS_ACCESS_TOKEN']
api_token = os.environ.get('AIRTABLE_API_KEY')
headers = {
'Authorization': api_token
}
response = requests.get(attachment_url, headers=headers, allow_redirects=True)
if response.status_code != 200:
print(f"Failed to download attachment. Status Code: {response.status_code}")
return None
file_content = response.content
if not file_content:
print("No content in file")
return None
print(f"Size of the content retrieved: {len(file_content)} bytes")
file_name = attachment_url.split("/")[-1]
content_type = mimetypes.guess_type(file_name)[0] or 'application/octet-stream'
print(f"Guessed Content-Type: {content_type}")
upload_url = f"{targetprocess_domain}/UploadFile.ashx?access_token={targetprocess_access_token}"
files = {'file': (file_name, file_content, content_type)}
data = {'generalId': bug_id}
try:
response = requests.post(upload_url, files=files, data=data)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as error:
print(f'Error uploading attachment to Targetprocess: {error}')
return None