Chilkat Online Tools

Objective-C / Atlassian Confluence Cloud / Add labels to content

Back to Collection Items

#import <CkoHttp.h>
#import <CkoJsonArray.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.h>
#import <CkoHttpResponse.h>
#import <NSString.h>

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

CkoHttp *http = [[CkoHttp alloc] init];
BOOL success;

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

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

// [
//   {
//     "prefix": "<string>",
//     "name": "<string>"
//   },
//   {
//     "prefix": "<string>",
//     "name": "<string>"
//   }
// ]

CkoJsonArray *jarr = [[CkoJsonArray alloc] init];

[jarr AddObjectAt: [NSNumber numberWithInt: -1]];
CkoJsonObject *jsonObj_1 = [jarr ObjectAt: [NSNumber numberWithInt: ([jarr.Size intValue] - 1)]];
[jsonObj_1 UpdateString: @"prefix" value: @"<string>"];
[jsonObj_1 UpdateString: @"name" value: @"<string>"];

[jarr AddObjectAt: [NSNumber numberWithInt: -1]];
jsonObj_1 = [jarr ObjectAt: [NSNumber numberWithInt: ([jarr.Size intValue] - 1)]];
[jsonObj_1 UpdateString: @"prefix" value: @"<string>"];
[jsonObj_1 UpdateString: @"name" value: @"<string>"];

// Adds the "Authorization: Bearer <access_token>" header.
http.AuthToken = @"<access_token>";
[http SetRequestHeader: @"Content-Type" value: @"application/json"];

CkoStringBuilder *sbRequestBody = [[CkoStringBuilder alloc] init];
[jarr EmitSb: sbRequestBody];

CkoHttpResponse *resp = [http PTextSb: @"POST" url: @"https://your-domain.atlassian.net/wiki/rest/api/content/:id/label" textData: sbRequestBody charset: @"utf-8" contentType: @"application/json" md5: NO gzip: NO];
if (http.LastMethodSuccess == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
[resp GetBodySb: sbResponseBody];

CkoJsonObject *jResp = [[CkoJsonObject alloc] init];
[jResp LoadSb: sbResponseBody];
jResp.EmitCompact = NO;

NSLog(@"%@",@"Response Body:");
NSLog(@"%@",[jResp Emit]);

int respStatusCode = [resp.StatusCode intValue];
NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
if (respStatusCode >= 400) {
    NSLog(@"%@",@"Response Header:");
    NSLog(@"%@",resp.Header);
    NSLog(@"%@",@"Failed.");

    return;
}

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "results": [
//     {
//       "prefix": "Duis elit dolor officia",
//       "name": "non sunt",
//       "id": "nulla veniam",
//       "label": "deserunt Ut Excepteur magna"
//     },
//     {
//       "prefix": "in consequat deserun",
//       "name": "officia cillum enim",
//       "id": "Lorem quis esse amet",
//       "label": "dolore esse"
//     }
//   ],
//   "start": -32316568,
//   "limit": -3993073,
//   "size": -78086889,
//   "_links": {}
// }

// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

NSString *prefix = 0;
NSString *name = 0;
NSString *id = 0;
NSString *label = 0;

int start = [[jResp IntOf: @"start"] intValue];
int limit = [[jResp IntOf: @"limit"] intValue];
int size = [[jResp IntOf: @"size"] intValue];
int i = 0;
int count_i = [[jResp SizeOfArray: @"results"] intValue];
while (i < count_i) {
    jResp.I = [NSNumber numberWithInt: i];
    prefix = [jResp StringOf: @"results[i].prefix"];
    name = [jResp StringOf: @"results[i].name"];
    id = [jResp StringOf: @"results[i].id"];
    label = [jResp StringOf: @"results[i].label"];
    i = i + 1;
}

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '[
    {
        "prefix": "<string>",
        "name": "<string>"
    },
    {
        "prefix": "<string>",
        "name": "<string>"
    }
]'
https://your-domain.atlassian.net/wiki/rest/api/content/:id/label

Postman Collection Item JSON

{
  "name": "Add labels to content",
  "request": {
    "auth": {
      "type": "oauth2"
    },
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "[\n    {\n        \"prefix\": \"<string>\",\n        \"name\": \"<string>\"\n    },\n    {\n        \"prefix\": \"<string>\",\n        \"name\": \"<string>\"\n    }\n]"
    },
    "url": {
      "raw": "{{baseUrl}}/api/content/:id/label",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "content",
        ":id",
        "label"
      ],
      "variable": [
        {
          "key": "id",
          "value": "<string>",
          "type": "string",
          "description": "(Required) The ID of the content that will have labels added to it."
        }
      ]
    },
    "description": "Adds labels to a piece of content. Does not modify the existing labels.\n\nNotes:\n\n- Labels can also be added when creating content ([Create content](#api-content-post)).\n- Labels can be updated when updating content ([Update content](#api-content-id-put)).\nThis will delete the existing labels and replace them with the labels in\nthe request.\n\n**[Permissions](https://confluence.atlassian.com/x/_AozKw) required**:\nPermission to update the content."
  },
  "response": [
    {
      "name": "Returned if the labels are added to the content.",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: oauth2",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "<token>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "[\n    {\n        \"prefix\": \"<string>\",\n        \"name\": \"<string>\"\n    },\n    {\n        \"prefix\": \"<string>\",\n        \"name\": \"<string>\"\n    }\n]"
        },
        "url": {
          "raw": "{{baseUrl}}/api/content/:id/label",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "content",
            ":id",
            "label"
          ],
          "variable": [
            {
              "key": "id"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"results\": [\n  {\n   \"prefix\": \"Duis elit dolor officia\",\n   \"name\": \"non sunt\",\n   \"id\": \"nulla veniam\",\n   \"label\": \"deserunt Ut Excepteur magna\"\n  },\n  {\n   \"prefix\": \"in consequat deserun\",\n   \"name\": \"officia cillum enim\",\n   \"id\": \"Lorem quis esse amet\",\n   \"label\": \"dolore esse\"\n  }\n ],\n \"start\": -32316568,\n \"limit\": -3993073,\n \"size\": -78086889,\n \"_links\": {}\n}"
    },
    {
      "name": "Returned if;\n\n- There is no content with the given ID.\n- The calling user does not have permission to view the content.",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: oauth2",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "<token>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "[\n    {\n        \"prefix\": \"<string>\",\n        \"name\": \"<string>\"\n    },\n    {\n        \"prefix\": \"<string>\",\n        \"name\": \"<string>\"\n    }\n]"
        },
        "url": {
          "raw": "{{baseUrl}}/api/content/:id/label",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "content",
            ":id",
            "label"
          ],
          "variable": [
            {
              "key": "id"
            }
          ]
        }
      },
      "status": "Not Found",
      "code": 404,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/plain"
        }
      ],
      "cookie": [
      ],
      "body": ""
    }
  ]
}