0

I am using ZAPI (Zephyr API) to make get,post and put requests through groovy code and read data/update JIRA. While get and put requests are working fine via groovy code, i am facing issue in adding an attachment (Text File). Below is the code -

def attachmentRestUrl = new RESTClient("https://jira-XXXXXXXXXXX/rest/zapi/latest/attachment?entityId=993118&entityType=execution")
def emptyHeadersAttachment = [:]

//emptyHeadersAttachment."Content-Type" = "application/json"
//emptyHeadersAttachment."Accept"="application/json"
emptyHeadersAttachment."Authorization"="Basic DDDDDDDDD5nZ19RQVVDAKDALJDAyIUJ4ZGQwVWRPYUo="
emptyHeadersAttachment."X-Atlassian-Token"="nocheck"
def responseAttach = attachmentRestUrl.post(
   headers: emptyHeadersAttachment,
   body : [ file : 'C:\test.txt' ]
 //    contentType: JSON
 //  requestContentType: JSON
//}
)

I tried multiple options like multipart/form-body,changing requestcontenttype but getting "HTTPResponseException". Please note that same operation i am able to achieve using POSTMAN and cURL and tried multiple options with commenting/uncommenting sections. Please help !!!!

  • Why are you putting parameters in the URL? You would usually place that into the body as part of the Map – Graham H Apr 02 '18 at 06:55
  • Even if i put the parameters in body (in the path), then also the same error comes up. – Jaideep Bahadur Srivastava Apr 02 '18 at 07:49
  • 1
    Are you trying to add the file as the attachment, or just the name? – Graham H Apr 02 '18 at 08:06
  • Thanks Graham for replying, I am trying to add the file as an attachment – Jaideep Bahadur Srivastava Apr 02 '18 at 08:27
  • 1
    You have just created a JSON object that contains a key of "file" and a value of "test.txt". It looks like zapi is expecting a content type of multipart/form-data. Have a look at the documentation for create attachment for zapi – Graham H Apr 02 '18 at 08:50
  • Thanks Graham, Actually with Postman and cURL, i am able to add attachments like curl -D- -H "Authorization: Basic RW5nZ19RQVVQwVWRPYUo=" -X POST -H "X-Atlassian-Token: nocheck" -F "file=@C:\test.txt" "https://jira-intranet/rest/zapi/latest/attachment?entityId=%1&entityType=execution" and also in Postman, only by setting "authorization" and "X-Atlassian-Token" in the headers and file in the body. Not able to figure out the missing thing in groovy. also had tried multipart/form-data in the requestcontenttype. – Jaideep Bahadur Srivastava Apr 02 '18 at 09:23
  • I think you will need to place the file in a location that's accessible by the application, IE relative to the root of the application – Graham H Apr 02 '18 at 09:46
  • Thanks Graham for all the help. Will try that also, in case of success will surely update :) – Jaideep Bahadur Srivastava Apr 02 '18 at 10:17

0 Answers0