Chilkat Online Tools

Swift3 / DocuSign REST API / 7. Add an Attachment to Envelope

Back to Collection Items

func chilkatTest() {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!
    var success: Bool

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "attachments": [
    //     {
    //       "attachmentId": "123",
    //       "data": "{{data}}",
    //       "name": "sample_document",
    //       "anchorIgnoreIfNotPresent": "false",
    //       "anchorUnits": "inches"
    //     }
    //   ]
    // }

    let json = CkoJsonObject()!
    json.update("attachments[0].attachmentId", value: "123")
    json.update("attachments[0].data", value: "{{data}}")
    json.update("attachments[0].name", value: "sample_document")
    json.update("attachments[0].anchorIgnoreIfNotPresent", value: "false")
    json.update("attachments[0].anchorUnits", value: "inches")

    http.setRequestHeader("Content-Type", value: "application/json")
    // Adds the "Authorization: Bearer {{accessToken}}" header.
    http.authToken = "{{accessToken}}"

    let sbRequestBody = CkoStringBuilder()!
    json.emitSb(sbRequestBody)

    var resp: CkoHttpResponse? = http.pTextSb("PUT", url: "https://domain.com/{{apiVersion}}/accounts/{{accountId}}/envelopes/{{envelopeId}}/attachments", textData: sbRequestBody, charset: "utf-8", contentType: "application/json", md5: false, gzip: false)
    if http.lastMethodSuccess == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("\(resp!.statusCode.intValue)")
    print("\(resp!.bodyStr!)")
    resp = nil

}

Curl Command

curl -X PUT
	-H "Content-Type: application/json"
	-H "Authorization: Bearer {{accessToken}}"
	-d '{
    "attachments": [{
        "attachmentId": "123",
        "data": {{document}},
        "name": "sample_document",
        "anchorIgnoreIfNotPresent": "false",
        "anchorUnits": "inches"
    }]
}'
https://domain.com/{{apiVersion}}/accounts/{{accountId}}/envelopes/{{envelopeId}}/attachments

Postman Collection Item JSON

{
  "name": "7. Add an Attachment to Envelope",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Authorization",
        "value": "Bearer {{accessToken}}"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"attachments\": [{\n        \"attachmentId\": \"123\",\n        \"data\": {{document}},\n        \"name\": \"sample_document\",\n        \"anchorIgnoreIfNotPresent\": \"false\",\n        \"anchorUnits\": \"inches\"\n    }]\n}"
    },
    "url": {
      "raw": "{{baseUrl}}/{{apiVersion}}/accounts/{{accountId}}/envelopes/{{envelopeId}}/attachments",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "{{apiVersion}}",
        "accounts",
        "{{accountId}}",
        "envelopes",
        "{{envelopeId}}",
        "attachments"
      ]
    },
    "description": "In this example, we will make further changes to our existing envelope by adding an attachmen to it."
  },
  "response": [
  ]
}