Somehow cannot download complete attachment by REST API in Python

Jason Huang September 6, 2022

Hi,

 

I refer to Python requests, how to limit received size, transfer rate, and/or total time? - Stack Overflow and my code are as follows:


#!/usr/bin/python 2import time 3 4import requests 5from jira import JIRA 6 7PAT = "MYPAT" 8headers = JIRA.DEFAULT_OPTIONS["headers"].copy() 9headers["Authorization"] = f"Bearer {PAT}" 10myTicket = "JTE-311" 11jiraURL = "https://jira.accton.com:8443/rest/api/2/issue/" 12fileName = ( 13 "Docker.png" # In this case we'll be looking for a specific file in the attachments 14) 15 16attachment_final_url = "" # To validate if there are or not attachments 17initial_timeout = 115 18your_maximum = 2000000 19receive_timeout = 10000000000000 20 21 22def main(): 23 print(f"[ You are checking ticket: '{myTicket}']") 24 r = requests.get(jiraURL + myTicket, headers=headers, timeout=initial_timeout) 25 26 r.raise_for_status() 27 28 # if int(r.headers.get("Content-Length")) > your_maximum: 29 # raise ValueError("response too large") 30 31 size = 0 32 start = time.time() 33 34 for chunk in r.iter_content(1024): 35 if time.time() - start > receive_timeout: 36 raise ValueError("timeout reached") 37 38 size += len(chunk) 39 if size > your_maximum: 40 raise ValueError("response too large") 41 42 # do something with chunk 43 44 # If the status isn't 200 we leave 45 if not r.status_code == 200: 46 print("Error accesing JIRA:" + str(r.status_code)) 47 exit() 48 else: 49 data = r.json() 50 51 if not data["fields"]["attachment"]: 52 status_attachment = "ERROR: Nothing attached, attach a file named: " + fileName 53 attachment_final_url = "" 54 else: 55 for i in data["fields"]["attachment"]: 56 if i["filename"] == fileName: 57 attachment_final_url = i["content"] 58 status_attachment_name = ( 59 "OK: The desired attachment exists: " + fileName 60 ) 61 attachment_name = False 62 attachment_amount = False 63 attachment_files = False 64 break 65 else: 66 attachment_files = False 67 status_attachment_name = ( 68 "ERROR: None of the files has the desired name " 69 ) 70 attachment_final_url = "" 71 attachment_name = True 72 attachment_amount = True 73 continue 74 75 if attachment_final_url != "": 76 # with open(fileName, "wb+") as f: 77 # # f.write(r.content) 78 # # f.write(r.content.encode("utf8").decode("utf8")) 79 # f.write(r.content.decode("utf8").encode("utf8")) 80 # # f.write(r.content.decode("iso-8859-1").encode("utf8")) 81 # f.close() 82 r = requests.get(attachment_final_url, headers=headers, stream=True) 83 f = open(fileName, "wb") 84 for chunk in r.iter_content(chunk_size=512): 85 # for chunk in r.iter_lines(): 86 if chunk: 87 f.write(chunk) 88 else: 89 print(status_attachment) 90 91 92if __name__ == "__main__": 93 main()


And then the value of the “size” variable is 11476, but the size of the downloaded “Docker.png” is 62271 as well as the other downloaded attachment.

 

I also upload a 1.txt file that contains “123” words.

After downloading it, I found its content is as follows:


<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Oops - an error has occurred</title><script type="text/javascript">contextPath = "";</script><link type='text/css' rel='stylesheet' href='/static-assets/metal-all.css' media='all'><script src='/static-assets/jquery-min.js'></script><script src='/static-assets/metal-all.js'></script><meta name="decorator" content="none" /></head><body class=" error-page error500"><script type="text/javascript">document.body.className += " js-enabled";</script><div id="page"><header id="header" role="banner" aria-label="Site"></header><div id="content"><div class="aui-page-panel" ><div class="aui-page-panel-inner"><main role="main" id="main" class="aui-page-panel-content lowerContent" ><div id="error-state"><span class="error-type"></span><h1>Sorry, we had some technical problems during your last operation.</h1><div id="technical-details-content" class="technical-details "><p>Copy the content below and send it to your Jira Administrator</p><div class="technical-details-content short" contentEditable readonly><p class="referral">Log&#39;s referral number: <strong id="log-referral-id">0630b411-c24b-408f-a52a-e277a2fca623</strong></p></div></div><a href="javascript&colon;window.history.back()" class="aui-button">Return to the previous page</a></div></main></div></div></div><footer id="footer" role="contentinfo"><section class="footer-body"><ul class="atlassian-footer"> 2 <li> 3 Atlassian Jira <a class="seo-link" rel="nofollow" href="https://www.atlassian.com/software/jira">Project Management Software</a> 4 </li> 5 <li> 6 <a id="about-link" rel="nofollow" href="/secure/AboutPage.jspa/secure/AboutPage.jspa">About Jira</a> 7 </li> 8 <li> 9 <a id="footer-report-problem-link" rel="nofollow" href="/secure/CreateIssue!default.jspa">Report a problem</a> 10 </li> 11</ul> 12 <p class="atlassian-footer"> 13 <span class="licensemessage"> 14 15 </span> 16 </p> 17<div id="footer-logo"><a href="http://www.atlassian.com/" rel="nofollow">Atlassian</a></div></section></footer></div></body></html>


I’ve converted the 1.txt to 1.html and attached its screenshot for your reference.

We use 2FA for website login, will this be the root cause?

 

Thanks,

Jason

1 answer

0 votes
Prince Nyeche
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 7, 2022

From your code, you seem to be encoding and decoding which is wrong. If you're downloading content from Jira, the response will be in bytes, you simply just need to write that content into a file object.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.12
TAGS
AUG Leaders

Atlassian Community Events