Jira Cloud: Upload an Attachment
See more Jira Examples
Upload a local file as an issue attachment using multipart/form-data. Jira requires the form field name file and the header X-Atlassian-Token: no-check. Chilkat supplies the multipart boundary; do not construct it yourself.
Attachments must be enabled and the account must have permission to create attachments. The site attachment settings endpoint reports the upload limit. Re-running this example uploads another attachment; filenames are not unique identifiers. The returned attachment ID is useful for subsequent download or deletion.
The code uses an Atlassian account email and an API token without scopes with the site URL. For a scoped API token, keep Basic authentication and change baseUrl to https://api.atlassian.com/ex/jira/YOUR_CLOUD_ID. For OAuth2, use that gateway URL, omit Login/Password/BasicAuth, and set http.AuthToken to the access token. See API token authentication and OAuth2 authorization. The account must have permission for the operation; OAuth scopes do not grant additional Jira permissions.
Targets Jira Cloud. Jira Data Center has a separate REST API and may require different paths, authentication, or payloads.
Atlassian REST API documentation
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
string ls_BaseUrl
oleobject loo_Url
oleobject loo_Resp
oleobject loo_Reply
string ls_IssueKey
oleobject loo_Request
oleobject loo_Attachments
oleobject loo_Attachment
long i
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Http.RequireSslCertVerify = 1
loo_Http.BasicAuth = 1
// Read these credentials from private application configuration.
loo_Http.Login = "YOUR_ATLASSIAN_EMAIL"
loo_Http.Password = "YOUR_API_TOKEN"
ls_BaseUrl = "https://your-domain.atlassian.net"
loo_Http.Accept = "application/json"
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")
ls_IssueKey = "PROJECT-123"
loo_Http.SetUrlVar("issueKey",ls_IssueKey)
loo_Request = create oleobject
li_rc = loo_Request.ConnectToNewObject("Chilkat.HttpRequest")
loo_Request.HttpVerb = "POST"
loo_Request.ContentType = "multipart/form-data"
loo_Request.AddHeader("Accept","application/json")
loo_Request.AddHeader("X-Atlassian-Token","no-check")
li_Success = loo_Request.AddFileForUpload2("file","qa_data/report.pdf","application/pdf")
if li_Success <> 1 then
Write-Debug "Could not open the local attachment file."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
return
end if
loo_Url.SetString(ls_BaseUrl)
loo_Url.Append("/rest/api/3/issue/{$issueKey}/attachments")
li_Success = loo_Http.HttpReq(loo_Url.GetAsString(),loo_Request,loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "No HTTP response was received. Check connectivity and TLS settings."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Request failed. HTTP " + string(loo_Resp.StatusCode)
if loo_Resp.StatusCode = 429 then
Write-Debug "Rate limited. Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
end if
// Inspect this response locally; it can contain private Jira data.
Write-Debug loo_Resp.BodyStr
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
return
end if
loo_Attachments = create oleobject
li_rc = loo_Attachments.ConnectToNewObject("Chilkat.JsonArray")
li_Success = loo_Attachments.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected an array of uploaded attachments."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
destroy loo_Attachments
return
end if
loo_Attachment = create oleobject
li_rc = loo_Attachment.ConnectToNewObject("Chilkat.JsonObject")
i = 0
do while (i < loo_Attachments.Size)
loo_Attachment = loo_Attachments.ObjectAt(i)
Write-Debug "Uploaded: " + loo_Attachment.StringOf("filename") + " ID: " + loo_Attachment.StringOf("id")
destroy loo_Attachment
i = i + 1
loop
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Request
destroy loo_Attachments
destroy loo_Attachment