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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
String sBaseUrl
Handle hoUrl
Variant vResp
Handle hoResp
Handle hoReply
String sIssueKey
Variant vRequest
Handle hoRequest
s Handle hoAttachments
Handle hoAttachment
Integer i
String sTemp1
String sTemp2
Integer iTemp1
Integer iTemp2
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComBasicAuth Of hoHttp To True
// Read these credentials from private application configuration.
Set ComLogin Of hoHttp To "YOUR_ATLASSIAN_EMAIL"
Set ComPassword Of hoHttp To "YOUR_API_TOKEN"
Move "https://your-domain.atlassian.net" To sBaseUrl
Set ComAccept Of hoHttp To "application/json"
Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
If (Not(IsComObjectCreated(hoUrl))) Begin
Send CreateComObject of hoUrl
End
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatJsonObject)) To hoReply
If (Not(IsComObjectCreated(hoReply))) Begin
Send CreateComObject of hoReply
End
Move "PROJECT-123" To sIssueKey
Get ComSetUrlVar Of hoHttp "issueKey" sIssueKey To iSuccess
Get Create (RefClass(cComChilkatHttpRequest)) To hoRequest
If (Not(IsComObjectCreated(hoRequest))) Begin
Send CreateComObject of hoRequest
End
Set ComHttpVerb Of hoRequest To "POST"
Set ComContentType Of hoRequest To "multipart/form-data"
Send ComAddHeader To hoRequest "Accept" "application/json"
Send ComAddHeader To hoRequest "X-Atlassian-Token" "no-check"
Get ComAddFileForUpload2 Of hoRequest "file" "qa_data/report.pdf" "application/pdf" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not open the local attachment file."
Procedure_Return
End
Get ComSetString Of hoUrl sBaseUrl To iSuccess
Get ComAppend Of hoUrl "/rest/api/3/issue/{$issueKey}/attachments" To iSuccess
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoRequest to vRequest
Get pvComObject of hoResp to vResp
Get ComHttpReq Of hoHttp sTemp1 vRequest vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "No HTTP response was received. Check connectivity and TLS settings."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Request failed. HTTP " iTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 429) Begin
Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
Showln "Rate limited. Retry-After: " sTemp1
End
// Inspect this response locally; it can contain private Jira data.
Get ComBodyStr Of hoResp To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonArray)) To hoAttachments
If (Not(IsComObjectCreated(hoAttachments))) Begin
Send CreateComObject of hoAttachments
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoAttachments sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected an array of uploaded attachments."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoAttachment
If (Not(IsComObjectCreated(hoAttachment))) Begin
Send CreateComObject of hoAttachment
End
Move 0 To i
While (i < (ComSize(hoAttachments)))
Get ComObjectAt Of hoAttachments i To vAttachment
If (IsComObject(vAttachment)) Begin
Get Create (RefClass(cComChilkatJsonObject)) To hoAttachment
Set pvComObject Of hoAttachment To vAttachment
End
Get ComStringOf Of hoAttachment "filename" To sTemp1
Get ComStringOf Of hoAttachment "id" To sTemp2
Showln "Uploaded: " sTemp1 " ID: " sTemp2
Send Destroy of hoAttachment
Move (i + 1) To i
Loop
End_Procedure