Cannot copy attachment using REST API

I’m trying to copy an attachment from a blogpost to another using the REST API:
https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-child-attachment-post

this is the groovy script I’m using

def attId = 'att457998341'
def att = get('/wiki/rest/api/content/' + attId).asObject(Map).body
def downloadUrl = '/wiki' + att['_links']['download'] // --> /wiki/download/attachments/458129410/att.txt?version=1&modificationDate=1532601762568&cacheVersion=1&api=v2

// more info: if I access the downloadUrl from the browser, I'm able to download the attachment


def targetBlogpostId = '458194948'
def response = Unirest.post("/wiki/rest/api/content/" + targetBlogpostId + "/child/attachment")
                .header("X-Atlassian-Token", "nocheck")
                .header("Accept", "application/json")
                .field("file", downloadUrl)
                .field("minorEdit", "true")
                .asObject(Map)

but I get a negative response saying:

{
  "delegate": {
    "headers": {
      "Strict-Transport-Security": [
        "max-age=315360000; includeSubDomains; preload"
      ],
      "transfer-encoding": [
        "chunked"
      ],
      "Server": [
        "Atlassian Proxy/0.1.284"
      ],
      "X-Content-Type-Options": [
        "nosniff"
      ],
      "X-AUSERNAME": [
        "admin"
      ],
      "correlationId": [
        "ccb3c941-8cfa-4bf0-8c9d-3caeecd368f7"
      ],
      "X-XSS-Protection": [
        "1; mode=block"
      ],
      "Date": [
        "Thu, 26 Jul 2018 11:48:48 GMT"
      ],
      "Content-Type": [
        "text/plain"
      ]
    },
    "statusText": "Unsupported Media Type",
    "rawBody": "",
    "parsingError": {
      "present": true
    },
    "body": null,
    "status": 415
  },
  "headers": {
    "Strict-Transport-Security": [
      "max-age=315360000; includeSubDomains; preload"
    ],
    "transfer-encoding": [
      "chunked"
    ],
    "Server": [
      "Atlassian Proxy/0.1.284"
    ],
    "X-Content-Type-Options": [
      "nosniff"
    ],
    "X-AUSERNAME": [
      "admin"
    ],
    "correlationId": [
      "ccb3c941-8cfa-4bf0-8c9d-3caeecd368f7"
    ],
    "X-XSS-Protection": [
      "1; mode=block"
    ],
    "Date": [
      "Thu, 26 Jul 2018 11:48:48 GMT"
    ],
    "Content-Type": [
      "text/plain"
    ]
  },
  "status": 415,
  "statusText": "Unsupported Media Type",
  "rawBody": "",
  "body": null,
  "parsingError": {
    "present": true
  }
}

After a good amount of trials I was able to copy an attachment from a blogpost to another using the REST API using the following script:

def attId = 'att457998341'
def att = Unirest.get('/wiki/rest/api/content/' + attId).asObject(Map).body
def downloadUrl = '/wiki' + att['_links']['download']

def targetBlogpostId = '458194948'
def readFileResp = get(downloadUrl).asBinary()

def response = Unirest.post("/wiki/rest/api/content/" + targetBlogpostId + "/child/attachment")
                    .header("X-Atlassian-Token", "nocheck")
                    .header("Accept", "application/json")
                    .field("file", readFileResp.body, att.title.toString())
                    .asObject(Map)
1 Like