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 PureBasic Downloads
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonArray.pb"
IncludeFile "CkHttpRequest.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkStringBuilder.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkRequireSslCertVerify(http, 1)
CkHttp::setCkBasicAuth(http, 1)
; Read these credentials from private application configuration.
CkHttp::setCkLogin(http, "YOUR_ATLASSIAN_EMAIL")
CkHttp::setCkPassword(http, "YOUR_API_TOKEN")
baseUrl.s = "https://your-domain.atlassian.net"
CkHttp::setCkAccept(http, "application/json")
url.i = CkStringBuilder::ckCreate()
If url.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
reply.i = CkJsonObject::ckCreate()
If reply.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
issueKey.s = "PROJECT-123"
CkHttp::ckSetUrlVar(http,"issueKey",issueKey)
request.i = CkHttpRequest::ckCreate()
If request.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpRequest::setCkHttpVerb(request, "POST")
CkHttpRequest::setCkContentType(request, "multipart/form-data")
CkHttpRequest::ckAddHeader(request,"Accept","application/json")
CkHttpRequest::ckAddHeader(request,"X-Atlassian-Token","no-check")
success = CkHttpRequest::ckAddFileForUpload2(request,"file","qa_data/report.pdf","application/pdf")
If success <> 1
Debug "Could not open the local attachment file."
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkHttpRequest::ckDispose(request)
ProcedureReturn
EndIf
CkStringBuilder::ckSetString(url,baseUrl)
CkStringBuilder::ckAppend(url,"/rest/api/3/issue/{$issueKey}/attachments")
success = CkHttp::ckHttpReq(http,CkStringBuilder::ckGetAsString(url),request,resp)
If CkHttpResponse::ckStatusCode(resp) = 0
Debug "No HTTP response was received. Check connectivity and TLS settings."
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkHttpRequest::ckDispose(request)
ProcedureReturn
EndIf
If (success <> 1) OR (CkHttpResponse::ckStatusCode(resp) < 200) OR (CkHttpResponse::ckStatusCode(resp) >= 300)
Debug "Request failed. HTTP " + Str(CkHttpResponse::ckStatusCode(resp))
If CkHttpResponse::ckStatusCode(resp) = 429
Debug "Rate limited. Retry-After: " + CkHttpResponse::ckGetHeaderField(resp,"Retry-After")
EndIf
; Inspect this response locally; it can contain private Jira data.
Debug CkHttpResponse::ckBodyStr(resp)
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkHttpRequest::ckDispose(request)
ProcedureReturn
EndIf
attachments.i = CkJsonArray::ckCreate()
If attachments.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonArray::ckLoad(attachments,CkHttpResponse::ckBodyStr(resp))
If success <> 1
Debug "Expected an array of uploaded attachments."
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkHttpRequest::ckDispose(request)
CkJsonArray::ckDispose(attachments)
ProcedureReturn
EndIf
attachment.i = CkJsonObject::ckCreate()
If attachment.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
i.i = 0
While (i < CkJsonArray::ckSize(attachments))
attachment = CkJsonArray::ckObjectAt(attachments,i)
Debug "Uploaded: " + CkJsonObject::ckStringOf(attachment,"filename") + " ID: " + CkJsonObject::ckStringOf(attachment,"id")
CkJsonObject::ckDispose(attachment)
i = i + 1
Wend
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkHttpRequest::ckDispose(request)
CkJsonArray::ckDispose(attachments)
CkJsonObject::ckDispose(attachment)
ProcedureReturn
EndProcedure